The xml_set_element_handler() function specifies the function to be called at the beginning and end of elements in an XML document.
If successful, the function returns TRUE. If it fails, returns FALSE.
xml_set_element_handler(parser,start,end)
parameter | describe |
---|---|
parser | Required. Specifies the XML parser to use. |
start | Required. Specifies a function to be called at the beginning of an element. |
end | Required. Specifies the function to be called at the end of the element. |
The function specified by the "start" parameter must have three parameters:
parameter | describe |
---|---|
parser | Required. Specifies a variable containing the XML parser that calls the processor. |
name | Required. Specifies a variable containing the name (as a string) of the element in the XML file that triggers this function. |
data | Required. Specifies an array containing element attributes (as strings) in the XML file. |
The function specified by the "end" parameter must have two parameters:
parameter | describe |
---|---|
parser | Required. Specifies a variable containing the XML parser that calls the processor. |
name | Required. Specifies a variable containing the name (as a string) of the element in the XML file that triggers this function. |
Note: The start and end parameters can be an array containing object references and method names.
<?php$parser=xml_parser_create();function start($parser,$element_name,$element_attrs) { switch($element_name) { case "NOTE": echo "-- Note --<br />"; break; case "TO": echo "To: "; break; case "FROM": echo "From: "; break; case "HEADING": echo "Heading: "; break; case "BODY": echo "Message: "; } }function stop($parser,$element_name) { echo "<br />"; }function char($parser,$data) { echo $data; }xml_set_element_handler($parser,"start" ,"stop");xml_set_character_data_handler($parser,"char");$fp=fopen("test.xml","r");while ($data=fread($fp,4096)) { xml_parse($parser,$data,feof($fp)) or die (sprintf("XML Error: %s at line %d", xml_error_string(xml_get_error_code($parser )), xml_get_current_line_number($parser))); }xml_parser_free($parser);?>
The output of the above code looks like this:
-- Note --To: ToveFrom: JaniHeading: ReminderMessage: Don't forget me this weekend!