Latest web development tutorials

PHP xml_set_default_handler () function

PHP XML Reference Complete PHP XML Reference

Definition and Usage

xml_set_default_handler () function for the XML parser to establish a default data processor.

The function specified in the function as long as the parser to find the data in the XML file will be called.

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

grammar

xml_set_default_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 default($parser,$data)
{
echo $data;
}

xml_set_default_handler($parser,"default");
$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!

If you "view source" in the browser window to select, you will see the following HTML:

<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>


PHP XML Reference Complete PHP XML Reference