PHP
  Home arrow PHP arrow Page 5 - 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 - Watching the XML Parsing Events: Callback Functions


    (Page 5 of 9 )

    What do we expect to happen when the above code is executed? Well, each time the xml_parse function encounters an XML tag in our document it'll fire an event by calling the functions we told it to call.

    The term for this behavior is often referred to as a 'Callback Function'. Basically we want PHP to call us back at a given function name each time it triggers an event of a certain type.

    By using the function, xml_set_element_handler, we are letting the PHP parser know that the open tag should invoke a method in our class named 'startElement' and all close tags should invoke a method in our class named 'endElement':


    xml_set_element_handler($this->parser, "startElement", "endElement");


    Additionally, we want to capture all the character data between tags, so we use the method, xml_set_character_data_handler to define the callback function as 'characterData':

    xml_set_character_data_handler($this->parser, "characterData");


    Callback functions are a very powerful tool that many languages offer and they work great in this specific case. Until I write an article on using callback functions, just accept that it 'simply works', and let's see what events are fired as we parse our sample XML document:


    START: [drive]
    DATA: []
    DATA: [ ]
    START: [folder]
    DATA: []
    DATA: [ ]
    START: [file]
    END: [file]
    DATA: []
    DATA: [ ]
    START: [file]
    END: [file]
    DATA: []
    DATA: [ ]
    END: [folder]
    DATA: []
    DATA: [ ]
    START: [folder]
    DATA: []
    DATA: [ ]
    START: [file]
    END: [file]
    DATA: []
    DATA: [ ]
    START: [file]
    DATA: []
    DATA: [ This is a comment about file d.]
    DATA: []
    DATA: [ We like comments.]
    END: [file]
    DATA: []
    DATA: [ ]
    END: [folder]
    DATA: []
    END: [drive]


    Did you expect to see that? Notice that each time a tag is opened, we see the 'START: [tagname]' line printed. When a tag is closed, we see the 'END: [tagname]' lines. Finally, whenever data is encountered, we get the 'DATA: [...]' lines.

    Notice, though that the data lines are not necessarily together. Rules in parsing say that you can not guarantee that the data will always be together in one chunk. In fact, it's likely that it will NOT be together.

    The PHP parser is allowed to call your characterData callback method as many times as it needs to and you'll have to concat the strings together until the end tag is closed.

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