PHP xml_set_element_handler() Function
Complete PHP XML Reference Manual
Definition and Usage
The xml_set_element_handler() function specifies the functions to call at the start and end of elements in an XML document.
On success, this function returns TRUE. On failure, it returns FALSE.
Syntax
xml_set_element_handler(parser,start,end)
| Parameters | Description |
|---|---|
| parser | Required. Specifies the XML parser to use. |
| start | Required. Specifies the function to call at the start of an element. |
| end | Required. Specifies the function to call at the end of an element. |
The function specified by the "start" parameter must have three parameters:
| Parameters | Description |
|---|---|
| parser | Required. Specifies a variable containing the XML parser that called the handler. |
| name | Required. Specifies a variable containing the name of the element in the XML file (as a string) that triggered this function. |
| data | Required. Specifies an array containing the element attributes from the XML file (as strings). |
The function specified by the "end" parameter must have two parameters:
| Parameters | Description |
|---|---|
| parser | Required. Specifies a variable containing the XML parser that called the handler. |
| name | Required. Specifies a variable containing the name of the element in the XML file (as a string) that triggered this function. |
Tips and Notes
**Note:** The start and end parameters can be an array containing an object reference and a method name.
Example
"; 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 ""; } 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 code above is shown below:
-- Note --
To: Tove
From: Jani
Heading: Reminder
Message: Don't forget me this weekend!
Complete PHP XML Reference Manual