Callback function implementation

Now you know how they work, here's the prototype for the opening and closing element callback functions. As I said, the parameter list is fixed - you can rename the parameters, but there must be the same amount. You can, of course, call the function whatever name you please. Note that $parser is a reference to the XML parser object you created above.

function startElement($parser, $el_name, $attributes) { }
function endElement($parser, $el_name) { }

The callback function prototype for the character data handler works in almost the same way as the endElement function you just saw:

function charData($parser, $chardata) { }

When registered with PHP, this will automatically be called every time character data is encountered in an XML document.

In order to notify PHP of the functions you wish to use for XML events, two new functions have to be called, passing in the names of the functions you wish to use as callbacks. These functions are xml_set_element_handler() (to set the start end element event handlers) and xml_set_character_data_handler() to set the character data hander.

Here is an example of them being used to register the startElement and endElement functions shown above:

xml_set_element_handler($parser, "startElement", "endElement");
xml_set_character_data_handler($parser, "charData");

As you can see, the first parameter for both functions is the XML parser reference, $parser. For xml_set_element_handler(), the second and third parameters are the names of the callback functions we wish to use to handle elements. For xml_set_character_data_handler(), there is only a second parameter, and it is the name of the function to use for character data.

Once we have basic functions written for startElement and endElement, and have registered those functions using xml_set_element_handler(), we're almost able to parse an XML document.

Here are very simple functions for startElement and endElement - feel free to modify them to do more exciting things!

function startElement($parser, $el_name, $attributes) {
    print "Opened element $el_name - it has . " count($attributes) . " parameters.<br />";
}

function endElement($parser, $el_name) {
    print "Closed element $el_name.<br />";
}

xml_set_element_handler($parser, "startElement", "endElement");

As you can see, nothing fancy - we simply print out the name of the element that was encountered, then, if available, we print out the number of attributes in the attribute list parameter.

In order to have events fired when character data is parsed, we need to fill in our skeleton charData function and register it with PHP. This is done like so:

function charData($parser, $chardata) {
    print "Parsed character data - $chardata</br>";
}

xml_set_character_data_handler($parser, "charData");

Again, we simply print out the contents of the parameter passed in. Simple!

 

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: Event-based XML parsing, at last! >>

Previous chapter: Getting to know callback functions

Jump to:

 

Home: Table of Contents

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