xml set character data handle in PHP

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

xml_set_character_data_handler() function in PHP

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

Syntax

xml_set_character_data_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_character_data_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_character_data_handler() function can be used in PHP.

<html>

<body>

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

    <?php

    $xml_parser=xml_parser_create();

    function char($xml_parser,$xmldata)

    {

     echo $xmldata;

     }

    xml_set_character_data_handler($xml_parser,"char");

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

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

     {

     xml_parse($xml_parser,$xmldata,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-character-data-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.