📜  PHP | xml_get_error_code()函数

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

PHP | xml_get_error_code()函数

xml_get_error_code()函数是PHP中的一个内置函数,用于返回 XML 解析器生成的错误代码。

句法:

int xml_get_error_code( resource $xml_parser )

参数:此函数接受所需的单个参数$xml_parser 。它指定要使用的 XML 解析器。
返回值:此函数在成功时返回 XML 解析器生成的错误代码,在失败时返回 False。

笔记:

  • 此函数适用于PHP 4.0.0 及更新版本。
  • 这些示例可能不适用于在线 IDE。因此,尝试在本地服务器或PHP托管服务器上运行它。

gfg.xml(标签不匹配错误):

XML


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


PHP
Error Code: " .
             
            // Error code
            xml_get_error_code($xml_parser) .
             
            "
Line: " .                           // Line number where the error occurred             xml_get_current_line_number($xml_parser) .                           "
Column: " .                           // Column number where the error occurred             xml_get_current_column_number($xml_parser) .                           "
Byte Index: " .                           // Byte index where the current byte occurred             xml_get_current_byte_index($xml_parser) . "
"         );     } }   // Free to XML parser xml_parser_free($xml_parser);   ?>


XML


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


PHP
Error Code: " .
             
            // Error code
            xml_get_error_code($xml_parser) .
             
            "
Line: " .                           // Line number where the error occurred             xml_get_current_line_number($xml_parser) .                           "
Column: " .                           // Column number where the error occurred             xml_get_current_column_number($xml_parser) .                           "
Byte Index: " .                           // Byte index where the current byte occurred             xml_get_current_byte_index($xml_parser) . "
"         );     } }   // Free to XML parser xml_parser_free($xml_parser);   ?>


方案一:

PHP

Error Code: " .
             
            // Error code
            xml_get_error_code($xml_parser) .
             
            "
Line: " .                           // Line number where the error occurred             xml_get_current_line_number($xml_parser) .                           "
Column: " .                           // Column number where the error occurred             xml_get_current_column_number($xml_parser) .                           "
Byte Index: " .                           // Byte index where the current byte occurred             xml_get_current_byte_index($xml_parser) . "
"         );     } }   // Free to XML parser xml_parser_free($xml_parser);   ?>

输出:

ERROR: Mismatched tag
Error Code: 76
Line: 7
Column: 13
Byte Index: 208

geeks.xml 文件(错误:字符串未用双引号括起来):

XML



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

方案二:

PHP

Error Code: " .
             
            // Error code
            xml_get_error_code($xml_parser) .
             
            "
Line: " .                           // Line number where the error occurred             xml_get_current_line_number($xml_parser) .                           "
Column: " .                           // Column number where the error occurred             xml_get_current_column_number($xml_parser) .                           "
Byte Index: " .                           // Byte index where the current byte occurred             xml_get_current_byte_index($xml_parser) . "
"         );     } }   // Free to XML parser xml_parser_free($xml_parser);   ?>

输出:

ERROR: String not closed expecting " or '
Error Code: 34
Line: 1
Column: 38
Byte Index: 37

参考: https://www. PHP.net/manual/en/函数.xml-get-error-code。 PHP