📜  PHP | xml_parser_create()函数

📅  最后修改于: 2022-05-13 01:56:37.370000             🧑  作者: Mango

PHP | xml_parser_create()函数

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 文件:

HTML


     user123 
     firstname lastname 
     +91-9876543210 
     I am John Doe. Live in Kolkata, India. 


PHP


XML


     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($parser, $element_name) {     echo "
"; }   // Character handler function 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);   ?>


方案一:

PHP


输出:

user123 
firstname lastname 
+91-9876543210 
I am John Doe. Live in Kolkata, India. 

极客.xml 文件:

XML



     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($parser, $element_name) {     echo "
"; }   // Character handler function 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/函数.xml-parser-create。 PHP