0

my server PHP Version 5.1.6 domxml is enable but when i try to execute

// Start XML file, create parent node
$doc = domxml_new_doc("1.0");
$node = $doc->create_element("markers");
$parnode = $doc->append_child($node);

it return fatel error

Call to undefined function domxml_new_doc() 

my server php info is

    dom

DOM/XML enabled
DOM/XML API Version 20031129
libxml Version  2.6.26
HTML Support    enabled
XPath Support   enabled
XPointer Support    enabled
Schema Support  enabled
RelaxNG Support enabled
harsha.kuruwita
  • 1,315
  • 12
  • 21
  • did you check if it's enabled in the SAPI version of PHP? looks like you're doing `phpinfo` at the command line, which uses a completely separate .ini file. – Marc B Oct 18 '13 at 17:34
  • @AmalMurali, PHP 4 was bundled with domxml, PHP 5 is not. So obviously the value will be false. – Shankar Narayana Damodaran Oct 18 '13 at 17:53
  • 1
    @ShankarDamodaran: Yeah, that command is only useful if you're on PHP4. Deleted it. :) – Amal Murali Oct 18 '13 at 17:54
  • @AmalMurali, You are pretty talented. All your answers are nice :) – Shankar Narayana Damodaran Oct 18 '13 at 17:58
  • Where should that function come from? Please provide reference that it exists. Also I do not think that Stackoverflow is a fitting venue to ask questions about longtime-dead PHP versions this year, neither it is for support requests (*"any one can help to fix this issue"*). This is rather special interest. I suggest to close against the duplicate. – hakre Oct 18 '13 at 18:29

2 Answers2

2

There are two common ways to do this.

DOMDocument

DOM XML was deprecated in favor of this. It follows the W3C specification so you have typical DOM methods such as getElementsByTagName, getElementById, appendChild, removeChild, etc. It also uses appropriate classes such as DOMElement, DOMNode and DOMNodeList.

$doc = new DOMDocument('1.0','utf-8');
$root = $doc->appendChild($doc->createElement('markers'));

// output
echo $doc->saveXML();

If you know already how to traverse the DOM in Javascript you pretty much already know how to do it in PHP using DOMDocument and other related classes.

I don't know why Shankar's answer was downvoted unless there are people who favor the next method.

SimpleXML

SimpleXML attempts to live up to its name by using only two classes and using a parent-child structure to traverse the document.

$doc = new SimpleXMLElement('<markers/>');

// output
echo $doc->asXML();

An element in itself evaluates to its contents if it's treated as a string, and attributes can be accessed as you would access elements of an associative array.

$marker = $doc->addChild('marker');
$marker->addAttribute('color','red');

You can also do this if you don't really need to hang onto the reference to the element. You can't add multiple attributes using this method, however, so you'd still need the previous to add new attributes without traversing the whole document.

$doc->addChild('marker')->addAttribute('color','red');

Access your elements and attributes like so:

// red
echo $doc->marker[0]['color'];

To set the element value just set it.

$doc->marker[0] = 'Text Value';

// Text Value
echo $doc->marker[0];
Dissident Rage
  • 2,610
  • 1
  • 27
  • 33
0

Why don't you make use of DOMDocument ?

Change your code

$doc = domxml_new_doc("1.0");
$node = $doc->create_element("markers");
$parnode = $doc->append_child($node);

to

$doc = new DOMDocument('1.0', 'iso-8859-1');
$node = $doc->createElement("markers");
$parnode = $doc->appendChild($node);
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126