PHP | xml_set_character_data_handler()函数
xml_set_character_data_handler()函数是PHP中的一个内置函数,用于为 XML 解析器设置字符数据处理函数。
句法:
bool xml_set_character_data_handler( resource $xml_parser, callable $data_handler )
参数:该函数接受上面提到的两个参数,如下所述:
- $xml_parser:必填参数。它持有 XML 解析器的引用来设置字符数据处理程序。
- $data_handler:必填参数。它是一个包含函数名称的字符串。
handler( resource $parser, string $data )
处理函数必须具有以下两个参数:
- $xml_parser:它保存正在调用处理程序的 XML 解析器的引用。
- $data:它将字符数据保存为字符串。
返回值:此函数在成功时返回 True,在失败时返回 False。
笔记:
- 此函数适用于PHP 4.0.0 及更新版本。
- 这些示例可能不适用于在线 IDE。因此,尝试在本地服务器或PHP托管服务器上运行它。
gfg.xml 文件:
XML
user123
firstname lastname
+91-9876543210
I am John Doe. Live in Kolkata, India.
PHP
PHP
USER DATA
";
break;
case "USERNAME":
echo "Username: ";
break;
case "NAME":
echo "Name: ";
break;
case "PHONE":
echo "Phone no: ";
break;
case "DETAIL":
echo "More about user: ";
}
}
// Element handler function named "ending_handler"
function ending_handler($xml_parser, $element_name) {
echo "
";
}
// Character handler function named "char_handler"
function char_handler($xml_parser, $data) {
echo $data;
}
// Setting element handlers
xml_set_element_handler($xml_parser,
"starting_handler", "ending_handler");
// Setting character data handler
xml_set_character_data_handler($xml_parser, "char_handler");
// Opening xml file
$file_pointer = fopen("gfg.xml", "r");
// Reading xml file
while ($data = fread($file_pointer, 4096)) {
xml_parse($xml_parser, $data, feof($file_pointer)) or
// Display error while xml parsing
die (sprintf("XML Error: %s at line %d",
// Error string
xml_error_string(xml_get_error_code($xml_parser)),
// Error line number
xml_get_current_line_number($xml_parser)));
}
// Free to xml parser
xml_parser_free($xml_parser);
// Closing file stream
fclose($file_pointer);
?>
方案一:
PHP
输出:
user123
firstname lastname
+91-9876543210
I am John Doe. Live in Kolkata, India.
方案二:
PHP
USER DATA
";
break;
case "USERNAME":
echo "Username: ";
break;
case "NAME":
echo "Name: ";
break;
case "PHONE":
echo "Phone no: ";
break;
case "DETAIL":
echo "More about user: ";
}
}
// Element handler function named "ending_handler"
function ending_handler($xml_parser, $element_name) {
echo "
";
}
// Character handler function named "char_handler"
function char_handler($xml_parser, $data) {
echo $data;
}
// Setting element handlers
xml_set_element_handler($xml_parser,
"starting_handler", "ending_handler");
// Setting character data handler
xml_set_character_data_handler($xml_parser, "char_handler");
// Opening xml file
$file_pointer = fopen("gfg.xml", "r");
// Reading xml file
while ($data = fread($file_pointer, 4096)) {
xml_parse($xml_parser, $data, feof($file_pointer)) or
// Display error while xml parsing
die (sprintf("XML Error: %s at line %d",
// Error string
xml_error_string(xml_get_error_code($xml_parser)),
// Error line number
xml_get_current_line_number($xml_parser)));
}
// Free to xml parser
xml_parser_free($xml_parser);
// Closing file stream
fclose($file_pointer);
?>
输出:
USER DATA
Username: user123
Name: firstname lastname
Phone no: +91-9876543210
More about user: I am John Doe. Live in Kolkata, India.
参考: https://www. PHP.net/manual/en/函数.xml-set-character-data-handler。 PHP