PHP4 included some basic XML parsing features, but PHP5 contains many new features and functions based on libxml2. Support for simpleXML in PHP5 is automatically turned on; there is no need to include any additional extensions. PHP5 also supports XML document validation when referencing a DTD or an XML Schema.
The SimpleXML Extension
The simpleXML extension is also experimental, but has been completely overhauled in PHP5. This extension is installed by default, with -enable-simplexml. It includes functions for working with XML documents that make common operations fairly easy, such as the ability to take a string and convert it to an XMLformatted document and display it. The primary advantage is that the XML document becomes an object that can be processed like other objects in PHP, with elements, attributes, and their data accessible using normal object operations (don't worry if you don't understand much about objects yet; they're discussed in greater detail in later chapters).
The functions include:
- simplexml_load_file: takes a file path as an argument, and if the contents of the file are well-formed XML will load the contents as an object.
- simplexml_load_string: takes a string as an argument, and the string should be well-formed XML. Converts the string to an object.
- simplexml_import_dom: takes a node from a DOM document and turns it into a simplexml node.
- simplexml_element->asXML: returns a well-formed XML string from a simpleXML object.
- simplexml_element->attributes: provides the attributes and values defines within a well-formed string of XML.
- simplexml_element->children: method provides the child elements of an element.
- simplexml_element->xpath: method runs an XPath query on a simpleXML node.
Using simplexml_load_string()
You can write out (or get as a result from an expression or function) a string that is formatted as well-formed XML within a PHP program. Once you have the string, you can turn it into a simpleXML object using the simplexml_load_string() function. To turn a string named $string into a simpleXML object based on a string of XML inside your PHP program, just do something like this:
<?php $string = <<<XML <?xml version='1.0'?> <root_element> <child01>My first element</child01> <child02>My second element</child02> </root_element> XML; $xml_string = simplexml_load_string($string); ?>
Try reading an XML string with the simplexml_load_string function first. All you need to do is supply a string to the function. And once you have your simpleXML object, you can use the object's asXML() function to display it as properly formatted XML.
Here's an example of using the simplexml_load_string and simplexml_element's asXML method. Open your text editor and create a. php document containing the following code:
<?php //The asXML method formats the parent object's data in XML version 1.0. //create an XML formatted string $my_xml_string = <<<XML <a> <b> <c>text content</c> <c>more text content</c> </b> <d> <c>even more text content</c> </d> </a> XML; //load the string into an object $xml_object = simplexml_load_string ($my_xml_string); //display the contents of the xml object echo $xml_object->asXML(); ?>
Save this document as create_xml_doc.php, and then open it in your browser.
Using simplxml_load_file()
Alternatively, if your XML happens to be in a file, you can turn the contents of the file into a simpleXML object using the simplexml_load_file() function. Whichever way you come up with your simpleXML object, it can be manipulated in the same way with the other simpleXML functions.
To simplify things, an external file is used to contain the XML strings, and read them with the simplexml_load_file() function. For example, if you want to read an XML file such as the one you created in the very beginning of the chapter, you can use the simplexml_load_file() function. Here's the XML file again (save it as php_programs.xml):
<?xml version="1.0" ?> <php_programs> <program name="cart"> <price>100</price> </program> <program name="survey"> <price>500</price> </program> </php_programs>
To read the names and values of the elements and attributes in this XML document, use the simpleXML function for loading a file: simplexml_load_file(). Just begin a PHP document (name this one simplexml_01.php), and then set a variable to the result of the simplexml_load_file() function, like this:
<?php $php_programs = simplexml load_file( 'php programs.xml');
The variable contains an object that can be used much like an ordinary array. To get the names and values from the variable, use the foreach statement to return keys and values, just like with an ordinary array:
foreach. ($php_programs->program as $program_key => $program_val) { echo "The root element. <B>php_programs</B> contains an element named <B>$program_ key</B><BR>";
But we can't directly echo out the contents of the $program_val variable; it contains an array-like object as well, and we must therefore use a foreach statement on it as well, both for its child elements and its attributes, like this:
foreach($program_val->children() as $child_of_a program key => $child of_program_val) { if ($child__of_program_key == "price") { foreach($program_val->attributes() as $att => $val.) { if ($att == "name") { foreach($program_val->price as $the_price) { echo "This <B>$program_key</B> element has an attribute named <B>$att</B> and is named <B>$val</B>.<BR>"; echo "This <B>$program_key</B> element has a child element named <B>$child_of_program_key</B> and the value of <B>$child_of_program_key</B> is <B>$the_price</B>.<BR>"; echo "Therefore, we can say that the <B>$child_of_program_key</B> of the <B>$val</B> <B>$program_key</B> is <B>\$$the_price</B>.<BR><BR>"; } } } } } } ?>
Changing a Value with simpleXML
Not only can you read the parts of an XML document into a simpleXML object, you can also change data in the in-memory document. All you need to do is properly address the data you want to change, and then set it equal to the value you want it to have, as you'll see in the following example.
Try it Out: Change the Value of a Node
Create a new file, save it as simplexml_change_value.php, and enter the following code into it:
<?php $xmlstr = <<<XML <pbp_programs> <program name="cart"> <price>100</price> </program> <program name="survey"> <price>500</price> </program> </php_programs> XML; $first_xml_string = simplexml_load.string ($xmlstr); $first_xml_string->program[0]->price = '250'; echo "<PRE>"; var_dump ($first_xml_string); echo "</PRE>"; ?>
How it Works
After you read in the XML string to the variable named $xmlstr, you can address the parts of the document in almost the same way as if it were an array, using array-style names and index numbers for each level. You know from the string you read in that program and price are elements, and that the first price encountered should be 100, but in the second line of code shown here, that value is reset to 250, and then the contents are dumped to the screen using the var_dump function (var_dump is a utility function in PHP for displaying the structure and data in PHP variables, no matter what kind they are). Notice we use the HTML <PRE> tags to maintain formatting of the dumped data.
$first_xml_string = simplexml_load_string($xmlstr); $first_xml_string->program[0]->price = '250'; echo "<PRE>"; var_dump($first_xml_string); echo "</PRE>";
Try it Out: Import a DOM with simpleXML
One of the nice things about simpleXML is how easy it is to get DOM objects into it, and vice versa. Create a new file and save it as simplexml_dom.php, with the following code:
<?php $xmlstr = <<<XML <php_programs> <program name="cart"> <price>100</price> </program> <program name="survey"> <price>500</price> </program> </php_programs> XML; $dom = new domDocument; $dom->loadXML($xmlstr); $s_dom = simplexml_import_dom($dom); echo "The price of the first program is <B>$" .$s_dom->program[0]->price . "</B>"; ?>
How it Works
In this example you first create a new XML document from the domDocument class by using the new keyword (more on creating object with the new keyword in the chapters about objects), and then you pull your XML string into the DOMDocument object using the loadXML function of the object. Finally, you echo out the contents of interest to you.
$dom = new domDocument;
$dom->loadXML($xmlstr);
$s_dom = simplexml_import_dom($dom);
echo "The price of the first program is <B>$"
. $s_dom->program[0]->price . "</B>";
