HomePHP Page 3 - Loading XML Strings with simpleXML in PHP 5
More examples with the "simplexml_load_string()" function: accessing parent and child nodes - PHP
Are you looking forward to learning about loading XML data strings, as well as searching and accessing nodes? Then this is the tutorial you’ve been waiting for since last week! Welcome to the second part of the series “Working with simpleXML in PHP 5.” This three-part series introduces the most important features of the “simpleXML” extension that comes with PHP 5, and shows by several hands-on examples how to implement the handy set of XML-parsing functions that are included in this library.
By using the XML string that I built in the previous section, it's possible to set up an example that shows how to access particular nodes within the given string and how to display them accordingly. With regard to this, the following example illustrates the process for retrieving the values of all the <email> nodes contained in the pertinent XML string:
// displays contents of <email> nodes require_once 'xml_string.php'; if(!$xml=simplexml_load_string($xmlstr)){ trigger_error('Error reading XML string',E_USER_ERROR); } echo 'Displaying email addresses of XML string...<br />'; foreach($xml as $user){ echo 'Values of email nodes: '.$user->email.'<br />'; }
As shown above, displaying the values of all the <email> nodes is actually a breeze. After loading the XML string onto an object, the corresponding nodes are accessed as object properties, which results in the following output:
Displaying email addresses of XML string... Values of email nodes: john@domain.com Values of email nodes: janet@janet-domain.com Values of email nodes: james@james-domain.com Values of email nodes: silvia@silvia-domain.com Values of email nodes: alejandro@alejandro-domain.com
All right, since the prior example speaks for itself, take a look at the next one, which shows how to locate a specific node within the XML string and displays the corresponding data using the typical array notation:
// locates a specific node and displays email of a given user require_once 'xml_string.php'; if(!$xml=simplexml_load_string($xmlstr)){ trigger_error('Error reading XML string',E_USER_ERROR); } // displays email of an user echo 'Displaying email of user '.$xml->user[2]->name.'<br />'; echo $xml->user[2]->email;
As you'll recall, when I explained how the "simplexml_load_file()" function worked, XML nodes can be easily accessed via a regular array syntax, as demonstrated by the example listed above. In this particular case, I opted to display data about the third <user> node:
Displaying email of user James Smith james@james-domain.com
Finally, let me show you one slightly more complex example that also uses the "simplexml_load_string()" function. For instance, suppose that you have the following XML string:
Essentially, what I did here was add an extra <gender> node to the previous XML string, which has a "type" attribute. Now, to my own pleasure and of course yours, the "simpleXML" extension provides you with the ability to access node attributes as associative arrays. Want to see how this is done? The following example clearly shows how this process is performed:
// example accessing attributes as array elements require_once 'xml_string.php'; if(!$xml=simplexml_load_string($xmlstr)){ trigger_error('Error reading XML string',E_USER_ERROR); } echo 'Displaying gender attribute of second user...<br />'; foreach($xml->user[0]->gender as $gender){ echo 'Gender: '.$gender['type'].'<br />'; }
As you can see, the above script first loads the corresponding XML string onto the $xml object, then reaches the second user, and finally displays the values of the <type> attributes associated with the respective <gender> nodes. Attributes are accessed via an associative array notation, as listed below:
echo 'Gender: '.$gender['type'].'<br />';
Lastly, the above expression displays the following information:
Displaying gender attribute of second user... Gender: male Gender: female
Definitely, accessing nodes and attributes is a no-brainer process if you use the "simpleXML" extension, which has been hopefully demonstrated by the hands-on examples you learned before. Since all the code samples I wrote have followed a procedural approach, in the last section of this article I'll build up a simple XML parsing class that utilizes the "simplexml_load_string()" function for parsing XML strings.
To find out how this will be achieved, go ahead and read the next section.