PHP
  Home arrow PHP arrow Page 3 - Loading XML Strings with simpleXML in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Loading XML Strings with simpleXML in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 9
    2006-06-19


    Table of Contents:
  • Loading XML Strings with simpleXML in PHP 5
  • Parsing basic XML strings: using the "simplexml_load_string()" function
  • More examples with the "simplexml_load_string()" function: accessing parent and child nodes
  • Parsing XML strings by an object-oriented approach: building a PHP 5 XML parsing class

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Loading XML Strings with simpleXML in PHP 5 - More examples with the "simplexml_load_string()" function: accessing parent and child nodes
    ( Page 3 of 4 )

    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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek