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 version="1.0" encoding="utf-8"?>
<user>
<username> user123 </username>
<name> firstname lastname </name>
<phone> +91-9876543210 </phone>
<detail> I am John Doe. Live in Kolkata, India. </detail>
</user>
程序1:
<?php
// Create an XML parser
$xml_parser = xml_parser_create();
// Set the character handler function for XML parser
xml_set_character_data_handler($xml_parser, "char_print");
// Opening xml file
$file_pointer = fopen("gfg.xml", "r");
// Reading xml data from file
while($data = fread($file_pointer, 4096)) {
// Parsing XML data
xml_parse($xml_parser, $data, feof($file_pointer)) or
// Display error when parsing error occurs
die (sprintf("XML Error: %s at line %d",
// Error string
xml_error_string(xml_get_error_code($xml_parser)),
// Current line
xml_get_current_line_number($xml_parser)));
}
// Free to xml parser
xml_parser_free($xml_parser);
fclose($file_pointer);
// Character handler function for XML parser
function char_print($xml_parser, $data_to_print) {
echo $data_to_print;
}
?>
輸出:
user123 firstname lastname +91-9876543210 I am John Doe. Live in Kolkata, India.
程序2:
<?php
// Create an xml parser
$xml_parser = xml_parser_create();
// Element handler function named "starting_handler"
// enables custom manupulation for output
function starting_handler($xml_parser,
$element_name, $element_attrs) {
switch($element_name) {
case "USER":
echo "<u>USER DATA</u><br>";
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 "<br>";
}
// 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/function.xml-set-character-data-handler.php
相關用法
- p5.js sq()用法及代碼示例
- d3.js d3.map.has()用法及代碼示例
- PHP next()用法及代碼示例
- p5.js day()用法及代碼示例
- p5.js pow()用法及代碼示例
- CSS var()用法及代碼示例
- d3.js d3.map.set()用法及代碼示例
- PHP pow( )用法及代碼示例
- PHP pi( )用法及代碼示例
- PHP Ds\Map get()用法及代碼示例
- PHP Ds\Map put()用法及代碼示例
- p5.js str()用法及代碼示例
注:本文由純淨天空篩選整理自gekcho大神的英文原創作品 PHP | xml_set_character_data_handler() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。