Home arrow PHP arrow Page 4 - Converting XML Into a PHP Data Structure
PHP

Converting XML Into a PHP Data Structure


Dante explains how to convert data from XML to the more PHP friendly arrays and arrays in arrays so that it can be used easily in your code.

Author Info:
By: Dante Lorenso
Rating: 4 stars4 stars4 stars4 stars4 stars / 60
January 06, 2003
TABLE OF CONTENTS:
  1. · Converting XML Into a PHP Data Structure
  2. · A Look at the XML File Structure
  3. · Using an Array as a Data Structure
  4. · Make PHP Do The Hard Work
  5. · Watching the XML Parsing Events: Callback Functions
  6. · Building the Array Tree
  7. · The Completed XMLToArray Class
  8. · How to Use the Class
  9. · Conclusion

print this article
SEARCH DEVARTICLES

Converting XML Into a PHP Data Structure - Make PHP Do The Hard Work
(Page 4 of 9 )

PHP has a built-in process for parsing your XML document. You pass a string to the xml_parse function with XML text in it and when the XML document is parsed, handlers for the configured events are called as many times as necessary.

Some events for which you can write handlers are 'StartElement', 'EndElement', and 'CharacterData'.

Here is some sample code for defining a class and the three event handlers to parse XML:

class XMLToArray
{
var $parser;

//-----------------------------------------
/* Parse a text string containing valid XML into a multidim array. */
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");

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

// ...
}

//-----------------------------------------
function startElement($parser, $name, $attrs)
{

// Start a new Element. This means we push the new element onto
// the stackand reset it's properties.
printf("START: [%s]\n", $name);

// ...
}

//-----------------------------------------
function endElement($parser, $name)
{

// End an element. This is done by popping the last element from
// the stack and adding it to the previous element on the stack.
printf("END: [%s]\n", $name);

// ...
}

//-----------------------------------------
function characterData($parser, $data) {
// Collect the data onto the end of the current chars.
printf("DATA: [%s]\n", str_replace("\n", "", $data));

// ...
}

//-----------------------------------------
}


Once we've built this class to wrap the PHP parser, we can create an instance of the class and have it parse the XML sample code we described above.

Some sample code to do this would look as follows:

$xml2a = new XMLToArray();
$xml2a->parse($xml_text);

blog comments powered by Disqus
PHP ARTICLES

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- 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

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials