📜  PHP | xml_parser_free()函数

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

PHP | xml_parser_free()函数

先决条件: XML 基础
xml_parser_free()函数是PHP中的一个内置函数,用于释放 XML 解析器。

句法:

bool xml_parser_free( resource $parser )

参数:此函数接受所需的单个参数$parser 。它指定了 XML 解析器对 free 的引用。
返回值:此函数在成功时返回 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($parser, $element_name) {     echo "
"; }   // Character handler function named "char_handler" function char_handler($parser, $data) {     echo $data; }   // Setting 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 $filePointer = fopen("gfg.xml", "r");   // Reading xml file while( $data = fread($filePointer, 4096) ) {        xml_parse($parser, $data, feof($filePointer)) 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($filePointer);   ?>


方案一:

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($parser, $element_name) {     echo "
"; }   // Character handler function named "char_handler" function char_handler($parser, $data) {     echo $data; }   // Setting 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 $filePointer = fopen("gfg.xml", "r");   // Reading xml file while( $data = fread($filePointer, 4096) ) {        xml_parse($parser, $data, feof($filePointer)) 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($filePointer);   ?>

输出:

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-free。 PHP