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

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
  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

Converting XML Into a PHP Data Structure - Using an Array as a Data Structure
(Page 3 of 9 )

Knowing that structure of the XML file is a tree, we need to find the best way to represent that “tree” data in PHP. Well, my first idea is to immediately consider a PHP array. Another option might be to build objects similar to the DOM parser approach. I've decided not to write a DOM parser, though (which you could easily do) because the DOM support is coming along quickly enough. Why duplicate their efforts?

For simple XML, PHP arrays are perfect for the task because you can create arrays of arrays of arrays and hence build a tree structure; exactly what we need for this learning exercise. Besides, there already exists a plethora of functions built into the core PHP language for iterating through arrays, pushing, popping, shifting, unshifting, splitting, joining, slicing, etc.

To use the DOM model for inspiration, though, we'll need to store several pieces of information about a given XML tag.

Each tag in XML will contain 4 pieces of information that we want to store:
  1. name of the tag,
  2. tag attributes (keys and values),
  3. data (the content inside the tag open and close),
  4. and possibly other nested tags.

A PHP array that can represent this simple XML tag (also referred to as a node in the tree) might look as follows:

$node = array();
$node["_NAME"] = 'folder'; // stores the node (tag) name
$node["_DATA"] = 'content'; // stores the text content inside tags
$node["_ELEMENTS"] = array(); // stores sub-nodes in order
$node["key1"] = "value1"; // stores all other node attributes
$node["key2"] = "value2"; // stores all other node attributes
$node["key3"] = "value3"; // stores all other node attributes

What I've done here is create an array of key and value pairs for all the attributes in the node. Then, I've created 3 internal-use-only keys called '_NAME', '_DATA', '_ELEMENTS' to store the tag name, tag data, and sub-node array.

By using the underscore ('_') I ensure that I'll not conflict with an attribute name. Using the sub-node array, we can now create arrays of arrays of arrays and basically build our tree.

Using our XML example again, suppose you wanted to read in some information from the file where name is 'd.txt'... You'd first convert the XML into a PHP array of arrays and then access the data with code like the following:

$file_name = $data['drive'][0]['folder'][1]['file'][1]['name'];
$owner = $data['drive'][0]['folder'][1]['file'][1]['owner'];
$comment = $data['drive'][0]['folder'][1]['file'][1]['_DATA'];

blog comments powered by Disqus

- 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 

Developer Shed Affiliates


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