PHP
  Home arrow PHP arrow Page 3 - Introducing SimpleXML in PHP 5
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 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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 / 32
    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 - Accessing file nodes as array elements


    (Page 3 of 4 )

    As I said before, after loading an XML file with the “simplexml_load_file()” function, it’s possible to access file nodes as array elements, which makes searching specific nodes a no-brainer task. In order to demonstrate this excellent functionality, I coded below a basic example that shows how to locate a specific node within the XML file I used before, and also displays additional data. Please take a look at this code listing:

    // locates a specific node and displays name of first user
    if(!$xml=simplexml_load_file('users.xml')){
        trigger_error('Error reading XML file',E_USER_ERROR);
    }
    // displays first user's name
    echo 'Displaying name of first user...<br />';
    echo $xml->user[0]->name;

    As illustrated above, once the contents of the sample “users.xml” XML file have been loaded, the script locates the first user and displays his full name:

    Displaying name of first user...
    John Doe

    As you can see, specific file nodes can be easily accessed by using a regular array syntax, as the line below demonstrates:

    echo $xml->user[0]->name;

    If you examine the above expression, you’ll understand how the first user is located (user[0]) and how that user's full name is properly displayed (user[0]->name). As you’ll probably realize, the “simpleXML” extension allows you to locate a given node easily within an XML file, as you would with regular arrays, and then access other sub nodes.

    Based on the same “users.xml” XML file that you saw previously, here is the correct expression for accessing the last <user> node of the respective file and  displaying its email address:

    // locates a specific node and displays name of last user
    if(!$xml=simplexml_load_file('users.xml')){
        trigger_error('Error reading XML file',E_USER_ERROR);
    }
    // displays last user's email address
    echo 'Displaying email address of last user...<br />';
    echo $xml->user[4]->email;

    Actually, the entire process for locating file nodes is reduced to navigating the three nodes, as illustrated in the previous example, so you shouldn’t have any problems accessing different nodes within a given XML file.

    At this point, I’m pretty sure that you learned the basics of loading XML files and navigating across file nodes, since the “simpleXML” extension is very understandable. Considering this, in the next few lines I’ll build a simple PHP 5 class, which will behave as a simple wrapper for the “simplexml_load_file()” function, performing basic parsing of XML files. To find out how this will be achieved, please click on the link below and keep on reading.

    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

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT