PHP
  Home arrow PHP arrow Page 3 - Converting XML Into a PHP Data Structure
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
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

Converting XML Into a PHP Data Structure
By: Dante Lorenso
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 33
    2003-01-06

    Table of Contents:
  • Converting XML Into a PHP Data Structure
  • A Look at the XML File Structure
  • Using an Array as a Data Structure
  • Make PHP Do The Hard Work
  • Watching the XML Parsing Events: Callback Functions
  • Building the Array Tree
  • The Completed XMLToArray Class
  • How to Use the Class
  • Conclusion

  • 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


    Converting XML Into a PHP Data Structure - Using an Array as a Data Structure


    (Page 3 of 9 )

    Knowing that structure of the XML file is a tree, we need to find the best way to represent that “tree” data in PHP. Well, my first idea is to immediately consider a PHP array. Another option might be to build objects similar to the DOM parser approach. I've decided not to write a DOM parser, though (which you could easily do) because the DOM support is coming along quickly enough. Why duplicate their efforts?

    For simple XML, PHP arrays are perfect for the task because you can create arrays of arrays of arrays and hence build a tree structure; exactly what we need for this learning exercise. Besides, there already exists a plethora of functions built into the core PHP language for iterating through arrays, pushing, popping, shifting, unshifting, splitting, joining, slicing, etc.

    To use the DOM model for inspiration, though, we'll need to store several pieces of information about a given XML tag.

    Each tag in XML will contain 4 pieces of information that we want to store:
    1. name of the tag,
    2. tag attributes (keys and values),
    3. data (the content inside the tag open and close),
    4. and possibly other nested tags.

    A PHP array that can represent this simple XML tag (also referred to as a node in the tree) might look as follows:

    $node = array();
    $node["_NAME"] = 'folder'; // stores the node (tag) name
    $node["_DATA"] = 'content'; // stores the text content inside tags
    $node["_ELEMENTS"] = array(); // stores sub-nodes in order
    $node["key1"] = "value1"; // stores all other node attributes
    $node["key2"] = "value2"; // stores all other node attributes
    $node["key3"] = "value3"; // stores all other node attributes

    What I've done here is create an array of key and value pairs for all the attributes in the node. Then, I've created 3 internal-use-only keys called '_NAME', '_DATA', '_ELEMENTS' to store the tag name, tag data, and sub-node array.

    By using the underscore ('_') I ensure that I'll not conflict with an attribute name. Using the sub-node array, we can now create arrays of arrays of arrays and basically build our tree.

    Using our XML example again, suppose you wanted to read in some information from the file where name is 'd.txt'... You'd first convert the XML into a PHP array of arrays and then access the data with code like the following:

    $file_name = $data['drive'][0]['folder'][1]['file'][1]['name'];
    $owner = $data['drive'][0]['folder'][1]['file'][1]['owner'];
    $comment = $data['drive'][0]['folder'][1]['file'][1]['_DATA'];

    More PHP Articles
    More By Dante Lorenso


     

    PHP ARTICLES

    - Making Usage Statistics in PHP
    - Installing PHP under Windows: Further Config...
    - File Version Management in PHP
    - Statistical View of Data in a Clustered Bar ...
    - Creating a Multi-File Upload Script in PHP
    - Executing Microsoft SQL Server Stored Proced...
    - Code 10x More Efficiently Using Data Access ...
    - A Few Tips for Speeding Up PHP Code
    - The Modular Web Page
    - Quick E-Commerce with PHP and PayPal
    - Regression Testing With JMeter
    - Building an Iterator with PHP
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP







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