PHP xml_set_element_handler()Functions
Complete PHP XML Reference Manual
Definition and Usage
The xml_set_element_handler() function specifies the functions to be called at the start and end of elements in an XML document.
If successful, this function returns TRUE. If it fails, 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 be called when an element starts. |
| end | Required. Specifies the function to be called when an element ends. |
The function specified by the "start" parameter must have three parameters:
| Parameters | Description |
|---|---|
| parser | Required. Specifies a variable containing the XML parser that calls the handler. |
| name | Required. Specifies a variable containing the name of the element (as a string) in the XML file that triggers the function. |
| data | Required. Specifies an array containing the attributes of the element (as strings) in the XML file. |
The function specified by the "end" parameter must have two parameters:
| Parameters | Description |
|---|---|
| parser | Required. Specifies a variable containing the XML parser that calls the handler. |
| name | Required. Specifies a variable containing the name of the element (as a string) in the XML file that triggers the function. |
Tips and Notes
Note:The start and end parameters can be an array containing an object reference and a method name.
Example
<?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);
?>
$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 is shown below:
-- Note --
To: Tove
From: Jani
Heading: Reminder
Message: Don't forget me this weekend!
To: Tove
From: Jani
Heading: Reminder
Message: Don't forget me this weekend!
Complete PHP XML Reference Manual Other Extensions