PHP
  Home arrow PHP arrow Page 2 - 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 - A Look at the XML File Structure


    (Page 2 of 9 )

    XML files are designed to be validated against a DTD and store data in a format similar to something you'd see in an HTML document. All tags are just made up on the fly (as defined by a DTD) and can represent a tree structure. Here is an example of some XML:

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- This is just a comment, ignore it -->
    <drive desc="Letters and Numbers Harddrive">
      <folder name="folder01">
        <file name="a.txt"/>
        <file name="b.txt"></file>
      </folder>
      <folder name="folder02">
        <file name="c.txt"/>
        <file name="d.txt" owner="bob">
          This is a comment about file d.
          We like comments.</file>
      </folder>
    </drive>

    Now, if you look at this document, you'll notice that there exists one tag with two folder tags inside it. Also, each folder tag contains two file tags within them.

    This file creates a tree-like structure of data. Now, we would like to access this data from within PHP. There are many ways to get to this data from within PHP including:
    • manually parsing the XML file,
    • parsing the file with the PHP SAX parser,
    • using the XPath libraries to search and pull data,
    • or using the DOM parser

    The manual option is not our most robust solution, and the DOM support in PHP is still experimental. So, I've chosen to use the SAX parser route.

    However, unlike similar other solutions, I'd like to write an object in PHP that parses this XML document into a PHP data-structure so that I can access the data like any other PHP data instead of having to write a custom parser each time I use XML in an application.

    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