SimpleXML works like the DOM, with objects, takes all the XML document as a hierarchical tree, but unlike DOM, SimpleXML is more flexible and uses less memory because the XML elements are stored directly as a PHP variable (string type and array).
SimpleXML is fast and easy to use when performing basic tasks like: reading XML files, extracting data from XML strings, and editing nodes and attributes. It uses a minimum of code and has an intuitive form of data.
To use SimpleXML for reading or changing XML data, you must know the structure of the XML document (its elements and attributes).
<?xml version="1.0" encoding="utf-8"?> <books> <title name="Book title"> <author>Author name</author> <chapter>Title of the chapter one</chapter> <chapter>Title of the chapter two</chapter> </title> <title name="Another Book"> <author>Author book two</author> <chapter>Chapter name</chapter> <chapter>Another chapter</chapter> </title> </books>
<?php $xml_file = 'test.xml'; // define the path and name of the xml file // load the content of the XML file and create a new XML object $xmlobj = simplexml_load_file($xml_file); $title1 = $xmlobj->title[0]; // gets the first <title> element $chapters = $title1->chapter; // gets all <chapter> tags from the first <title> (in an array) // output the value of 'name' attribute of the first <title> echo $title1['name']; // loop through $chapters and print the value of each <chapter> stored in $chapters foreach ($chapters as $item) { echo '<br />'. $item; } ?>As you can see, SimpleXML works a lot with Arrays, the attribute names are keys in the array of the elements.
<?php $xml_file = 'test.xml'; // define the directory and the name of the xml file // load the content of the XML file and create a new XML object $xmlobj = simplexml_load_file($xml_file); // get and prints the name of the first element (root) echo 'root: '. $xmlobj->getName() . '<br />'; // loops through the children of root foreach($xmlobj->children() as $child) { echo $child->getName(). ': '. '<br />'; // print the name of the current child // if the child node has other children if($child->count()>1) { // loops through the child node, prints the name and the content of each $child2 foreach($child as $child2) { echo $child2->getName(). ': '. $child2. '<br />'; } } } ?>Output:
<?php $xml_file = 'test.xml'; // define the directory and the name of the xml file // load the content of the XML file and create a new XML object $xmlobj = simplexml_load_file($xml_file); $title2 = $xmlobj->title[1]; // gets the second <title> element $chapters = $title2->chapter; // gets all <chapter> tags from the second <title> (in an array) $title2['name'] = 'Modified title'; // change the value of the name attribute of the second <title> $chapters[] = 'The new chapter'; // adds a new chapter element in the $chapters array // adds the XML content in a string and outputs it $xml_doc = $xmlobj->asXML(); echo htmlentities($xml_doc); ?>- asXML() method returns a well-formed XML string (version 1.0) based on SimpleXML element.
<?xml version="1.0" encoding="utf-8"?> <books> <title name="Book title"> <author>Author name</author> <chapter>Title of the chapter one</chapter> <chapter>Title of the chapter two</chapter> </title> <title name="Modified title"> <author>Author book two</author> <chapter>Chapter name</chapter> <chapter>Another chapter</chapter> <chapter>The new chapter</chapter> </title> </books>- If you want to save the modified XML, just save the XML string (with file_put_contents() ).
<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