PHP
  Home arrow PHP arrow Page 6 - 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  
Mobile Linux 
App Generation ROI 
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 - Building the Array Tree


    (Page 6 of 9 )

    At this point, we have a functioning class that will parse an XML document and fire events.

    What we'll need to do now is modify the event handlers to build our array tree using the array structure we defined above.

    A simple algorithm for developing this code goes as follows:
    • Each time a 'START' event is called, build a new node. The node we are building is our array structure, but you might decide to build a Node object using your own custom objects. The START event contains the name and attributes of the tag. After building the node, push the new node onto a class-level stack so that we can add data and sub-nodes from the 'DATA' or 'END' elements later.
    • Each time a 'DATA' event is called, append the text to the end of the text inside the node which is currently on top of our stack.
    • Each time an 'END' event is called, we need to pop a node off the stack, finalize the node, and add it as a subnode to the node which is now on the top of the stack. This completes the depth-first build of our tree structure.


    Before we start parsing the XML, it might help to push a 'root' node onto an empty stack. This way, when the parsing is completed, we expect to find only the root node remaining on the stack with all the subnodes built beneath it.

    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 1 hosted by Hostway
    Stay green...Green IT