当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PHP xml_get_current_line_number()用法及代码示例


xml_get_current_line_number()函数是PHP中的内置函数,用于返回XML解析器的当前行号。

用法:

int xml_get_current_line_number( resource $xml_parser )

参数:该函数接受单个参数$xml_parser。它指定要使用的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> 
</users>
<?php 
  
// XML file containing mismatch tags 
$xml_file = 'gfg.xml'; 
  
// XML parser initialization 
$xml_parser = xml_parser_create(); 
  
// File opening in read mode 
$file_pointer = fopen($xml_file, 'r'); 
   
// Reading data from the file stream 
while ($xml_data = fread($file_pointer, 4096)) { 
      
    // Parse the data chunk 
    if(!xml_parse($xml_parser, $xml_data, 
                        feof($file_pointer))) { 
          
        // Displaying errors 
        die( print "ERROR: " .  
          
            // Error string 
            xml_error_string(xml_get_error_code($xml_parser)) .  
              
            "<br/>Error Code: " .  
              
            // Error code 
            xml_get_error_code($xml_parser) .  
              
            "<br/>Line: " .  
              
            // Line number where the error occured 
            xml_get_current_line_number($xml_parser) .  
              
            "<br/>Column: " .  
              
            // Column number where the error occured 
            xml_get_current_column_number($xml_parser) . 
              
            "<br/>Byte Index: " .  
              
            // Byte index where the current byte occured 
            xml_get_current_byte_index($xml_parser) . "<br/>"
        ); 
    } 
} 
  
// 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 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 
  
// XML file containing mismatch tags 
$xml_file = 'geeks.xml'; 
  
// XML parser initialization 
$xml_parser = xml_parser_create(); 
  
// File opening in read mode 
$file_pointer = fopen($xml_file, 'r'); 
   
// Reading data from the file stream 
while ($xml_data = fread($file_pointer, 4096)) { 
      
    // Parse the data chunk 
    if(!xml_parse($xml_parser, $xml_data, 
                        feof($file_pointer))) { 
          
        // Displaying errors 
        die( print "ERROR: " .  
          
            // Error string 
            xml_error_string(xml_get_error_code($xml_parser)) .  
              
            "<br/>Error Code: " .  
              
            // Error code 
            xml_get_error_code($xml_parser) .  
              
            "<br/>Line: " .  
              
            // Line number where the error occured 
            xml_get_current_line_number($xml_parser) .  
              
            "<br/>Column: " .  
              
            // Column number where the error occured 
            xml_get_current_column_number($xml_parser) . 
              
            "<br/>Byte Index: " .  
              
            // Byte index where the current byte occured 
            xml_get_current_byte_index($xml_parser) . "<br/>"
        ); 
    } 
} 
  
// Free to XML parser 
xml_parser_free($xml_parser); 
  
?>

输出:

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

参考: https://www.php.net/manual/en/function.xml-get-current-line-number.php



相关用法


注:本文由纯净天空筛选整理自gekcho大神的英文原创作品 PHP | xml_get_current_line_number() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。