PHP
  Home arrow PHP arrow Page 2 - Introducing SimpleXML in PHP 5
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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? 
PHP

Introducing SimpleXML in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 30
    2006-06-12

    Table of Contents:
  • Introducing SimpleXML in PHP 5
  • Reading XML files with the “simplexml_load_file()” function
  • Accessing file nodes as array elements
  • Creating a basic XML parsing class

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


    Introducing SimpleXML in PHP 5 - Reading XML files with the “simplexml_load_file()” function


    (Page 2 of 4 )

    In order to begin tasting the power of the “simpleXML” extension, I’ll start with a simple example, which shows how to read the contents of a basic XML file. This extension will allow you do this in different ways, but in this case, I’ll use the “simplexml_load_file()” function, in conjunction with the following sample XML file:

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

    Fine, now that I have a simple XML file to work with, have a look at the following script, which first reads the nodes of this file and next displays them by using a “foreach” loop:

    // displays all the file nodes
    if(!$xml=simplexml_load_file('users.xml')){
        trigger_error('Error reading XML file',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 />';
    }

    As you can see, the previous script loads the contents of the XML file by using the “simplexml_load_file()” function and then loops over the corresponding nodes, outputting the following values:

    Displaying contents of XML file...
    Name: John Doe Address: Binary Avenue 1234 FL Email: john@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

    That was really easy, right? The cool “simplexml_load_file()” function loads the contents of a specific XML file passed as an argument onto an object, which exposes the different file nodes as properties. In order to access the nodes in question, all you have to do is use the “->” notation, and you’re done.

    Now, let me go one step further and demonstrate how to access the contents of a single node. The example shown below teaches you how to display the contents of all the <name> nodes included within the previous XML file:

    // displays contents of <name> nodes
    if(!$xml=simplexml_load_file('users.xml')){
        trigger_error('Error reading XML file',E_USER_ERROR);
    }
    echo 'Displaying user names of XML file...<br />';
    foreach($xml as $user){
        echo 'Name: '.$user->name.'<br />';
    }

    In this case, all the <name> nodes are accessed as object properties, and displayed as follows:

    Displaying user names of XML file...
    Name: John Doe
    Name: Janet Smith
    Name: James Smith
    Name: Silvia Wilson
    Name: Alejandro Gervasio

    Definitely, you’ll have to agree with me that the “simpleXML” extension makes it really easy to parse XML files, at least when you only need to perform basic tasks on a given file, such as reading all of the file nodes or accessing a particular one.

    However, there are still more useful things to explore regarding the use of the “simplexml_load_file()” function. Therefore, in the next section of this tutorial, I’ll show you how to access a specific node within an XML file by using an array notation. Please continue reading to learn more.

    More PHP Articles
    More By Alejandro Gervasio


       · In the course of this first article, you'll learn how to use some of the most...
       · I enjoyed your article. All the xml files you used were stored on the server, is...
       · Hello JET:I'm happy to know you enjoyed my article. Thank you. Now, in response...
       · Nice article, I only found an mistake during the countNodes function.The $nodeName...
       · I'm glad to know that the article has been useful to you. Now, with reference to...
       · Hi,this solution doesn't work for me.if I remove an 'address' element from...
       · Hello Bob,Thank you for your comments. I tested the method and works just fine....
       · Hi,thank you for your answer.Tested it again and still get the same output...
       · Hi Bob,Thank you for your feedback. Okay, there was a small bug on the method....
       · Hi,thank you for your answer.In the meantime I also found a working...
       · Hi Bob,I'm glad to see you found an alternative solution to the one I provided...
       · can you share how you removed an entire node?thxwalter
       · Thanks for the comments on my PHP article. Basically, to remove a node you might...
     

       

    PHP ARTICLES

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway