📜  PHP | xml_parse()函数

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

PHP | xml_parse()函数

xml_parse()函数是PHP中的一个内置函数,用于解析 XML 文档。

句法:

int xml_parse( resource $xml_parser, string $xml_data, bool $is_final )

参数:此函数接受三个参数,如上所述,如下所述:

  • $xml_parser:必填参数。它指定要使用的 XML 解析器。
  • $xml_data:必填参数。它指定要解析的数据。
  • $is_final:可选参数。如果此参数的值设置为 True,则数据是此解析中发送的最后一条数据。

返回值:此函数在成功时返回 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, $xml_data) {     echo $xml_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 ($xml_data = fread($file_pointer, 4096)) {        xml_parse($xml_parser, $xml_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 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, $xml_data) {     echo $xml_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 ($xml_data = fread($file_pointer, 4096)) {        xml_parse($xml_parser, $xml_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 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 解析。 PHP