xml set element handler in PHP

In this article I will explain how the xml_set_element_handler() function can be used in PHP.
  • 2541

xml_set_element_handler() function in PHP

  • The xml_set_element_handler() function is specify functions to be called at the start and end of an element in the XML document.
  • The xml_set_element_handler() function returns TRUE on success.
  • The xml_set_element_handler() function returns FALSE on failure.

Syntax

xml_set_element_handler(parser,start,end)

Parameter

Parameter Description
parser This is the required parameter. It is specify for use to XML parser.
start This is the required parameter. It is specify for a function to be called at the start of an element.
end This is the required parameter. It is specify for a function to be called at the end of an element.

The xml_set_element_handler() function specified by the "start" parameter must have three parameters:

parameter

Description

parser

This is the required parameter. It is specify a variable containing the XML parser calling the handler.

name This is the required parameter. It is specify a variable containing the name of the elements, that triggers this function, from the XML file as a string.

data

This is the required parameter. It is specify an array containing the elements attributes from the XML file as a string.

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

parameter Description
parser This is the required parameter. It is specify a variable containing the XML parser calling the handler.
name This is the required parameter. It is specify a variable containing the name of the elements, that triggers this function, from the XML file as a string.

Example

The following example show to how the xml_set_element_handler() function can be used in PHP.

<html>

<body>

<h3 style="color: darkblue;">xml_set_element_handler() function example in PHP</h3>

    <?php

    $xml_parser=xml_parser_create();

    function start($xml_parser,$element_list,$element_attrs)

    {

    switch($element_list)

        {

        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($xml_parser,$element_list)

    {

    echo "<br />";

    }   

    function char($xml_parser,$data)

    {

    echo $data;

    }

    xml_set_element_handler($xml_parser,"start","stop");

    xml_set_character_data_handler($xml_parser,"char");

    $fp=fopen("parsertest.xml","r");

    while ($data=fread($fp,4096))

    {

    xml_parse($xml_parser,$data,feof($fp)) or

    die (sprintf("XML Error: %s at line %d",

    xml_error_string(xml_get_error_code($xml_parser)),

    xml_get_current_line_number($xml_parser)));

    }

    xml_parser_free($xml_parser);

    ?>

</body>

</html>

 

Output

xml-set-element-handler-php.gif
You may also want to read these related articles here
 
Ask Your Question 
 
Got a programming related question? You may want to post your question here
 
© 2019 DotNetHeaven. All rights reserved.