Home arrow PHP arrow Page 2 - Loading XML Strings with simpleXML in PHP 5

Parsing basic XML strings: using the "simplexml_load_string()" function - 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

In order to demonstrate how XML strings can be loaded onto regular objects for further processing, I'll use the sample XML file that I built in the first article, as the foundation for creating a basic XML data string. As you'll probably recall, this XML file looked like this:

<?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>
</user>
<user>
<name>Janet Smith</name>
<address>Crazy Bits Road 4568 CA</address>
<email>janet@janet-domain.com</email>
</user>
<user>
<name>James Smith</name>
<address>Socket Boulevard 7894 OH</address>
<email>james@james-domain.com</email>
</user>
<user>
<name>Silvia Wilson</name>
<address>Protocol Avenue 5652 NY</address>
<email>silvia@silvia-domain.com</email>
</user>
<user>
<name>Alejandro Gervasio</name>
<address>Boulevard of Objects 10101 AR</address>
<email>alejandro@alejandro-domain.com</email>
</user>
</users>

As you can see, the above XML file is pretty understandable, since it contains a few simple <user> nodes that also include some additional child nodes. Now, based on the structure of this file, I'm going to create a new file, which will contain 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>
</user>
<user>
<name>Janet Smith</name>
<address>Crazy Bits Road 4568 CA</address>
<email>janet@janet-domain.com</email>
</user>
<user>
<name>James Smith</name>
<address>Socket Boulevard 7894 OH</address>
<email>james@james-domain.com</email>
</user>
<user>
<name>Silvia Wilson</name>
<address>Protocol Avenue 5652 NY</address>
<email>silvia@silvia-domain.com</email>
</user>
<user>
<name>Alejandro Gervasio</name>
<address>Boulevard of Objects 10101 AR</address>
<email>alejandro@alejandro-domain.com</email>
</user>
</users>
XML;

Well, creating a sample XML string wasn't a big deal, right? Now that I have an XML data string to work with, I'll use the "simplexml_load_string()" function that comes with the "simpleXML" library to load the corresponding XML data onto a regular object. Take a look at the following example:

// example using 'simplexml_load_string()' function
// include XML string
require_once 'xml_string.php';
if(!$xml=simplexml_load_string($xmlstr)){
    trigger_error('Error reading XML string',E_USER_ERROR);
}
echo 'Displaying contents of XML file...<br />';
foreach($xml as $user){
    echo 'Name: '.$user->name.' Address: '.$user->address.'
Email: '.$user->email.'<br />';
}

In this case, after including the pertinent PHP file that contains the previous XML string (called "xml_string.php"), the above script uses the "simplexml_load_string()" function, first for loading XML data onto the $xml object, and then for displaying the values of the corresponding child nodes.

As you can see, these child nodes are accessed as object properties, which makes it extremely easy to traverse a given XML string, something similar to the examples that you learned regarding the implementation of the "simplexml_load_file()" function.

As you may have guessed, the output of the previous example is as follows:

Displaying contents of XML file...
Name: John Doe Address: Binary Avenue 1234 FL Email:
john@domain.com
Name: Janet Smith Address: Crazy Bits Road 4568 CA Email:
janet@janet-domain.com
Name: James Smith Address: Socket Boulevard 7894 OH Email:
james@james-domain.com
Name: Silvia Wilson Address: Protocol Avenue 5652 NY Email:
silvia@silvia-domain.com
Name: Alejandro Gervasio Address: Boulevard of Objects 10101 AR
Email: alejandro@alejandro-domain.com

Don't tell me that wasn't pretty cool! The "simplexml_load_string()" function will allow you to load an XML string onto an object and traverse it by a regular "foreach" loop. Are you starting to see why this library is called "simpleXML"?

At this point, you saw how easy it is to load XML strings onto objects, so let's move one and see how specific nodes can be accessed as regular object properties. That's exactly the subject of the next section, therefore click on the link below to learn more about this.



 
 
>>> 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: