Parsing XML using PHP4
By Burhan Khalid2005-08-16
Setting up tag handlers
First, lets write out function that will be called for an open tag. The name of the function can be any valid PHP function name. The function must accept three arguments, and they must be $parser, $name, $attribs.
$parser = handle to our parser
$name = name of the current tag
$attrib = an array containing any attributes of the current tag
We don’t have to worry about calling the function, the parser does that automatically as it goes through our XML file. With that in mind, lets write our start tag function, which we will call start_tag (how creative, I know).
PHP:
Next, we will write our function that will be called when an ending tag is reached. This function, like our opening tag function, can be of any name that’s valid in PHP. The ending tag function must take these parameters $parser, $name.
$parser = handle to our parser
$name = name of the current tag
Lets write our ending tag function (which we will call end_tag):
PHP:
- function end_tag($parser, $name) {
- echo "Reached ending tag ".$name."<br /><br />";
- }
We have now taken care of all the requirements for the xml_set_element_handler function, and now we can call it :
PHP:
- xml_set_element_handler($xmlparser, "start_tag", "end_tag");
Tutorial Pages:
» Parsing XML using PHP4
» XML File Structure
» Creating our XML File
» Parsing with PHP
» Creating our Parser
» Setting up tag handlers
» Setting up content (data) handlers
» Starting up the parser
» Creating the gallery
© 2004-2005 Burhan Khalid
