Home arrow PHP arrow Page 5 - 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 - 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.
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