WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

PHP xml_set_element_handler() function


Definition and usage

xml_set_element_handler () function specifies the function to call at the beginning and end of an element in an XML document.

If successful, the 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 at the beginning of the 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:

Parameters description
parser Required. Specifies a variable containing an XML parser that calls the processor.
name Required. Specifies a variable containing the name (as a string) of an element in an XML file that triggers the function.
data Required. Specifies an array containing element attributes (as strings) in an XML file

The function specified by the "end" parameter must have two parameters:

Parameters description
parser Required. Specifies a variable containing an XML parser that calls the processor.
name Required. Specifies a variable containing the name (as a string) of an element in an XML file that triggers the function.


Tips and comments

Note: The start and end parameters can be an array containing object references and method names.


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);
?>

The output of the above code is shown below´╝Ü

-- Note --
To: Tove
From: Jani
Heading: Reminder
Message: Don't forget me this weekend!