xml_parser_create()函数是PHP中的一个内置函数,用于创建XML解析器。
用法:
resource xml_parser_create( string $encoding )
参数:该函数接受可选的单个参数$encoding。它指定字符编码:
- 用于PHP 4中的输入/输出
- 仅用于PHP 5的输出
- 对于5.0.0和5.0.1,默认输出字符集为ISO-8859-1
- 从5.0.2起默认输出字符集为UTF-8
返回值:此函数返回成功使用的资源处理程序,如果成功,其他XML函数将使用该资源处理程序;如果失败,则返回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
$parser = xml_parser_create();
// set the character handler function
// for the XML parser
xml_set_character_data_handler($parser, "char_print");
// Opening the xml file
$filePointer = fopen("gfg.xml", "r");
// Reading xml data from file
while($data = fread($filePointer, 4096)) {
// Parseing XML data
xml_parse($parser, $data, feof($filePointer)) or
// Display error when parse error occurs
die(sprintf("XML Error: %s at line %d",
// Error string
xml_error_string(xml_get_error_code($parser)),
// Current line
xml_get_current_line_number($parser))
);
}
// Free to xml parser
xml_parser_free($parser);
fclose($filePointer);
// Character handler function for XML parser
function char_print($parser, $data) {
echo $data;
}
?>
输出:
user123 firstname lastname +91-9876543210 I am John Doe. Live in Kolkata, India.
geeks.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>
程序2:
<?php
// Creating an XML parser
$parser = xml_parser_create();
// Element handler function named "starting_handler"
// enables custom manupulation for output
function starting_handler($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($parser, $element_name) {
echo "<br>";
}
// Character handler funtion named "char_handler"
function char_handler($parser, $data) {
echo $data;
}
// Setting the element handlers
xml_set_element_handler($parser,
"starting_handler", "ending_handler");
// Setting character data handler
xml_set_character_data_handler($parser, "char_handler");
// Opening xml file
$fp = fopen("geeks.xml", "r");
// Reading xml file
while ($data = fread($fp, 4096)) {
xml_parse($parser, $data, feof($fp)) or
// Display error while xml parsing
die (sprintf("XML Error: %s at line %d",
// Error string
xml_error_string(xml_get_error_code($parser)),
// Error line number
xml_get_current_line_number($parser))
);
}
// Free to xml parser
xml_parser_free($parser);
// Closing file stream
fclose($fp);
?>
输出:
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-parser-create.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_parser_create() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。