Home arrow PHP arrow Page 2 - 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 - A Look at the XML File Structure
(Page 2 of 9 )

XML files are designed to be validated against a DTD and store data in a format similar to something you'd see in an HTML document. All tags are just made up on the fly (as defined by a DTD) and can represent a tree structure. Here is an example of some XML:

<?xml version="1.0" encoding="UTF-8"?>
<!-- This is just a comment, ignore it -->
<drive desc="Letters and Numbers Harddrive">
  <folder name="folder01">
    <file name="a.txt"/>
    <file name="b.txt"></file>
  </folder>
  <folder name="folder02">
    <file name="c.txt"/>
    <file name="d.txt" owner="bob">
      This is a comment about file d.
      We like comments.</file>
  </folder>
</drive>

Now, if you look at this document, you'll notice that there exists one tag with two folder tags inside it. Also, each folder tag contains two file tags within them.

This file creates a tree-like structure of data. Now, we would like to access this data from within PHP. There are many ways to get to this data from within PHP including:
  • manually parsing the XML file,
  • parsing the file with the PHP SAX parser,
  • using the XPath libraries to search and pull data,
  • or using the DOM parser

The manual option is not our most robust solution, and the DOM support in PHP is still experimental. So, I've chosen to use the SAX parser route.

However, unlike similar other solutions, I'd like to write an object in PHP that parses this XML document into a PHP data-structure so that I can access the data like any other PHP data instead of having to write a custom parser each time I use XML in an application.
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