Processing XML with PHP - Define the Callbacks
(Page 4 of 6 )
The processor will make use of callback functions to handle the opening tags, closing tags and content of the XML document. These functions are identified to the processor though use of xml_set_element_handler and xml_set_character_data_handler functions. Each accepts the parser handle and the names of the callback functions we'll need to write later.
<?php function opening_element($parser, $element, $attributes) { /* opening XML element callback function */
}
function closing_element($parser, $element) { /* closing XML element callback function */
}
function character_data($parser, $data) { /* callback function for character data */
}
xml_set_element_handler($parser, "opening_element", "closing_element"); xml_set_character_data_handler($parser, "character_data"); ?> |
Note that the callback functions receive arguments: the opening XML element callback function accepts the parser handle, the current element and the element's attributes. The closing element callback function accepts the parser handle and the current element. The callback function responsible for handling the character data accepts the parser handle as well as the data. The attributes accepted by the opening element function is an array, of which the attribute names act as keys.
Next: Read the XML Document >>
More Miscellaneous Articles
More By bluephoenix