xml set default handler in PHP

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

xml_set_default_handler() function in PHP

  • The xml_set_default_handler() function is used to set the default data handler for the XML parser.
  • The xml_set_default_handler() function specifies what function to be called whenever the parser finds data in the XML file.
  • The xml_set_default_handler() function returns TRUE on success.
  • The xml_set_default_handler() function returns FALSE on failure.

Syntax

xml_set_default_handler(parser,handler)

Parameter

Parameter Description
parser This is the required parameter. It is specify for use to XML parser.
handler This is the required parameter. It is specify for a function to be used as an event handler.

The xml_set_default_handler() function specified by the "handler" 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.

data

This is the required parameter. It is specify a variable containing the character data from the XML file as a string.

Example

XML File

<?xml version="1.0" encoding="ISO-8859-1"?>

<message>

       <to>User</to>

       <from>C-sharpcorner Team</from>

       <heading>Wishes</heading>

        <body>Have a nice day....!</body>

</message>

PHP File

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

<html>

<body>

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

    <?php

    $xml_parser=xml_parser_create();

    function default1($xml_parser,$data)

    {

    echo $data;

    }

    xml_set_default_handler($xml_parser,"default1");

    $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-default-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
 
© 2020 DotNetHeaven. All rights reserved.