PHP
  Home arrow PHP arrow Page 7 - 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 
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 / 38
    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 - The Completed XMLToArray Class


    (Page 7 of 9 )


    class XMLToArray
    {

    //-----------------------------------------
    // private variables
    var $parser;
    var $node_stack = array();

    //-----------------------------------------
    /** PUBLIC
    * If a string is passed in, parse it right away.
    */
    function XMLToArray($xmlstring="") {
    if ($xmlstring) return($this->parse($xmlstring));
    return(true);
    }

    //-----------------------------------------
    /** PUBLIC
    * Parse a text string containing valid XML into a multidimensional array
    * located at rootnode.
    */
    function parse($xmlstring="") {
    // set up a new XML parser to do all the work for us
    $this->parser = xml_parser_create();
    xml_set_object($this->parser, $this);
    xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
    xml_set_element_handler($this->parser, "startElement", "endElement");
    xml_set_character_data_handler($this->parser, "characterData");

    // Build a Root node and initialize the node_stack...
    $this->node_stack = array();
    $this->startElement(null, "root", array());

    // parse the data and free the parser...
    xml_parse($this->parser, $xmlstring);
    xml_parser_free($this->parser);

    // recover the root node from the node stack
    $rnode = array_pop($this->node_stack);

    // return the root node...
    return($rnode);
    }

    //-----------------------------------------
    /** PROTECTED
    * Start a new Element. This means we push the new element onto the stack
    * and reset it's properties.
    */
    function startElement($parser, $name, $attrs)
    {

    // create a new node...
    $node = array();
    $node["_NAME"] = $name;
    foreach ($attrs as $key => $value) {
    $node[$key] = $value;
    }

    $node["_DATA"] = "";
    $node["_ELEMENTS"] = array();

    // add the new node to the end of the node stack
    array_push($this->node_stack, $node);
    }

    //-----------------------------------------
    /** PROTECTED
    * End an element. This is done by popping the last element from the
    * stack and adding it to the previous element on the stack.
    */
    function endElement($parser, $name) {
    // pop this element off the node stack
    $node = array_pop($this->node_stack);
    $node["_DATA"] = trim($node["_DATA"]);

    // and add it an an element of the last node in the stack...
    $lastnode = count($this->node_stack);
    array_push($this->node_stack[$lastnode-1]["_ELEMENTS"], $node);
    }

    //-----------------------------------------
    /** PROTECTED
    * Collect the data onto the end of the current chars.
    */
    function characterData($parser, $data) {
    // add this data to the last node in the stack...
    $lastnode = count($this->node_stack);
    $this->node_stack[$lastnode-1]["_DATA"] .= $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-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek