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

12.2.3     Callback function implementation

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

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!





<< 12.2.2 Getting to know callback functions   12.2.4 Event-based XML parsing, at last! >>
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
<a href="http://www.php-web-developer.com">PHP Developer</a> - 07 Sep 2008

Pls make an elaborate description.

PHP Programmer India - 07 Sep 2008

If you kindly describe what callback function is by some example then it will be very helpful.

<a href="http://www.php-web-developer.com">PHP Developer</a>

A PHP User - 07 Sep 2008

go fuck yourself then you will know :p

ajw@techie.com - 07 Sep 2008

i had problem with building an xml object. when you want to extract character data to put it into a variable with the callback funtion xml_set_character_data_handler($parser, "charData");
function charData($parser, $chardata) {
print "Parsed character data - $chardata</BR>";
}
you have to do this
if (strlen($chardata) > 1)
$variable = $chardata


it took me a whole day to figure this one out.



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 four plus seven?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow