Getting to know callback functions

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.

 

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Callback function implementation >>

Previous chapter: Creating a parser

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.