Home arrow PHP arrow 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.

TABLE OF CONTENTS:
  1. Loading XML Strings with simpleXML in PHP 5
  2. Parsing basic XML strings: using the "simplexml_load_string()" function
  3. More examples with the "simplexml_load_string()" function: accessing parent and child nodes
  4. Parsing XML strings by an object-oriented approach: building a PHP 5 XML parsing class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 9
June 19, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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:

$xmlstr=<<<XML
<?xml version="1.0" encoding="iso-8859-1"?>
<users>
<user>
<name>John Doe</name>
<address>Binary Avenue 1234 FL</address>
<email>john@john-domain.com</email>
<gender type="male">1</gender>
<gender type="female">2</gender>
</user>
<user>
<name>Janet Smith</name>
<address>Crazy Bits Road 4568 CA</address>
<email>janet@janet-domain.com</email>
<gender type="male">1</gender>
<gender type="female">2</gender>
</user>
<user>
<name>James Smith</name>
<address>Socket Boulevard 7894 OH</address>
<email>james@james-domain.com</email>
<gender type="male">1</gender>
<gender type="female">2</gender>
</user>
<user>
<name>Silvia Wilson</name>
<address>Protocol Avenue 5652 NY</address>
<email>silvia@silvia-domain.com</email>
<gender type="male">1</gender>
<gender type="female">2</gender>
</user>
<user>
<name>Alejandro Gervasio</name>
<address>Boulevard of Objects 10101 AR</address>
<email>alejandro@alejandro-domain.com</email>
<gender type="male">1</gender>
<gender type="female">2</gender>
</user>
</users>
XML;

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.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: