The XML DOM (Document Object Model) is a standard set of objects for accessing and manipulating XML documents.
The DOM extension allows you to to process XML documents in PHP 5+, you can use it to get, change, add, or delete XML elements.
In the DOM, every element is transformed into an object, and is seen like a node. Each node can have content, child nodes and parent node, starting with the root element (tag).
<?php $xml_file = 'files/dom_example.xml'; // define the directory and the name of the xml file $xmlDoc = new DomDocument('1.0', 'utf-8'); // create a new DOM object $root = $xmlDoc->createElement('html'); // create the root element $root = $xmlDoc->appendChild($root); // adds the root element in the DOM object $body = $xmlDoc->createElement('body'); // create another element, 'body' $body = $root->appendChild($body); // adds 'body' as a child element in $root $body->setAttribute('bgcolor', '#e8e8fe'); // sets an attribute for the 'body' $div = $xmlDoc->createElement('div'); // create another element, 'div' $div = $body->appendChild($div); // adds 'div' as a child element in 'body' $text = $xmlDoc->createTextNode('coursesweb.net - PHP'); // create a text content $text = $div->appendChild($text); // adds the text content in 'p' // save the xml content stored in $xmlDoc if($xmlDoc->save($xml_file)) echo 'The dom_example.xml was created'; else echo 'Error: unable to write dom_example.xml'; ?>In the $xmlDoc variable (which is a DOM object) is created the content of the XML, defining and adding each element one by one.
<?xml version="1.0" encoding="utf-8"?> <html> <body bgcolor="#e8e8fe"> <div>coursesweb.net - PHP</div> </body> </html>
<?php $xml_file = 'files/dom_example.xml'; // define the directory and the name of the xml file // create a new XML object and load the content of the XML file $xmlDoc = new DOMDocument(); $xmlDoc->load($xml_file); $root = $xmlDoc->documentElement; // get the first child node (the root) $elms = $root->getElementsByTagName("*"); // gets all elements ("*") of root // loop through all elements stored in $elms foreach ($elms as $item) { // gets the name and the value of each $item $tag = $item->nodeName; $value = $item->nodeValue; // outputs the $tag and $value echo $tag. ' = '. $value . 'This code will output:
'; } ?>
<?php $xml_file = 'files/dom_example.xml'; // define the directory and the name of the xml file // create a new XML object and load the content of the XML file $xmlDoc = new DOMDocument(); $xmlDoc->load($xml_file); $root = $xmlDoc->documentElement; // get the first child node (the root) $elms = $root->getElementsByTagName("*"); // gets all elements ("*") in root $nr_elms = $elms->length; // gets the number of elements // loop through all elements stored in $elms for($i = 0; $i<$nr_elms; $i++) { $node = $elms->item($i); // gets the current node // if the name of the current $node is 'div', changes its value if($node->nodeName=='div') { $node->nodeValue = 'The new text value'; // sets and add a new DIV, in the same parent node $new_elm = $xmlDoc->createElement('div', 'This is the new inserted DIV'); $node->parentNode->appendChild($new_elm); } } // save the new xml content in the same file and output its structure if($xmlDoc->save($xml_file)) { echo htmlentities($xmlDoc->saveXML()); } ?>- item($i) - etrieves a node specified by index ($i).
<ul> <li>http://coursesweb.net/html/</li> <li>http://coursesweb.net/css/</li> </ul>
.some_class { display: list-item; }
var obj = { "courses": ["php", "javascript", "ajax"] }; var jsonstr = JSON.stringify(obj); alert(jsonstr); // {"courses":["php","javascript","ajax"]}
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>'; $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); $elm = $dochtml->getElementById("dv1"); echo $elm->nodeValue; // CoursesWeb.net