Hudzilla.org - the homepage of Paul Hudson
Contents > XML & XSLT Wish List | Report Bug | About Me ]

12.1     Introduction to XML

This is NOT the latest copy of this book; click here for the latest version.

As XML is not a binary format, it needs to be parsed by PHP before the data it holds can be used. PHP offers two very different ways to work with XML documents - event-based parsing, which makes use of the Expat library (as seen in Apache and Mozilla), and tree-based parsing (known as "DOM XML").

You may wonder why there are two options when it comes to XML parsing, so let me explain what the difference between the two is:

Event-based parsers work through XML documents and call a special function you specify every time a new XML item is found, for example "start of element found". PHP allows you to specify these callback functions for a variety of events, including start of element, end of element, character data, namespaces, and more.

Tree-based parsers parse the entire document into a virtual tree, accessible through the document object module (DOM). For example, if an XML document for a news site had many channels, and each channel had sections, and each section had news items, you could refer to document.channels[3].sections[1].newsitem[2].

Generally speaking, tree-based parsers are slower than event-based parsers, but they are more useful in the long run because you can navigate easily through the tree they create. It is possible, however, to use an event-based parser to create a tree by writing the appropriate code for each callback function.





<< 12 XML & XSLT   12.2 Event-based parsing >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is three plus ten?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow