PHP
  Home arrow PHP arrow Page 3 - 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  
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? 
PHP

Introducing SimpleXML in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 40
    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:
      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


    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
     

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT