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

12.2.2     Getting to know callback functions

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

The core of event-based XML parsing revolves around the use of callback functions, which, when registered with PHP, are called whenever the PHP parser reaches a new part of your XML document. These callback functions contain your own code, which means you can manipulate the data however you please. Here is a very short XML document to give you an idea how these callback functions might work:

<news>
PHP 6.0 has been released!
</news>

When this (incomplete) document is parsed by PHP, an event is fired for the start of the <news> element. If you have a function assigned to handle element starts, you will be sent information about the element that was encountered.

Continuing on, we have a line of character data, "PHP 6.0 has been released!", and the end of an element. Again, if you have callback functions assigned to handle these parts of XML, you will be sent information about the data that was encountered.

Finally, the parser reaches </news>, and calls the function registered to handle terminating elements.

These callback functions take a set number of parameters, and these are fixed. For example, when you create a function to be used when element starts are encountered, you have to allow it to take three parameters - a reference to the XML parser that encountered the element, the name of the element that was encountered, and finally an array of attributes that the element has.

Here is another example chunk of incomplete XML that demonstrates how the attribute array works:

<news type="programming">
PHP 6.0 has been released!
</news>

When this is parsed by PHP, our element start callback function is called when "<news" is reached, and the third parameter sent to the function is an array of attributes in the function. In the example above, the array would have just element, "type", and its value would be set to "programming". If there were more attributes for the element being passed, there would be more elements in the array parameter.

Owing to the fact that there are no attributes used in terminating tags (e.g.: </news>), the callback function for end elements only takes two parameters: a reference to the XML parser, and the name of the element being closed.





<< 12.2.1 Creating a parser: xml_parser_create() and xml_parser_free()   12.2.3 Callback function implementation >>
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 zero plus three?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow