Latest web development tutorials

PHP xml_set_character_data_handler () function

PHP XML Reference Complete PHP XML Reference

Definition and Usage

xml_set_character_data_handler () function for the XML parser to build character data processor.

This function prescribed function when the parser to find character data in the XML file when invoked.

If successful, the function returns TRUE. If it fails, it returns FALSE.

grammar

xml_set_character_data_handler(parser,handler)

参数 描述
parser 必需。规定要使用的 XML 解析器。
handler 必需。规定作为事件处理器使用的函数。

By the function "handler" parameter specified must have two parameters:

参数 描述
parser 必需。规定一个变量,包含调用处理器的 XML 解析器。
data 必需。规定一个变量,包含 XML 文件中字符串形式的字符数据。


Tips and Notes

NOTE: handler can also be a parameter that contains an array of object references and method names.


Examples

XML file

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

PHP code

<?php
$parser=xml_parser_create();

function char($parser,$data)
{
echo $data;
}

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 code above is as follows:

Tove Jani Reminder Don't forget me this weekend!


PHP XML Reference Complete PHP XML Reference