Skip to Content

You are here

Nice Examples of XML and DOMDocument

Its all about the DOMDocument and generating the XML from the same with examples. I have created some of the good documents with the examples for generating the xml and formatting the same. You will get most of the functions with the examples are written in the following post :
$dom = new DOMDocument('1.0', 'UTF-8');
$root = $dom->createElement('root');
echo $dom->saveXML();
1. Creating XML Document and Displaying :
Above code will generate the xml string with UTF-8 encoding of version 1.0. First parameter will specify xml version as of now its
1. Second parameter will specify the encoding version 1.0. new DOMDocument('1.0', 'UTF-8') will create the document object $root = createElement('root') will create the first node / root node of the document object and store it in $root variable. The name of the node can be specified anything replacing 'root' in the function.
$dom->appendChild($root) will add the child to the document ie. first node to the document.
echo $dom->saveXML(); will save the generated xml on the browser and display it in proper format.
Note : To display the generated XML we should use the IE.8.