Get the byte index for an XML parser in PHP

Description

The following code shows how to get the byte index for an XML parser.

Example


/*from w ww . j a  v a2 s.co  m*/
<?php
    //invalid xml file
    $xmlfile = 'test.xml';

    $xmlparser = xml_parser_create();

    // open a file and read data
    $fp = fopen($xmlfile, 'r');
    while ($xmldata = fread($fp, 4096)){
      // parse the data chunk
      if (!xml_parse($xmlparser,$xmldata,feof($fp))){
            die( print "ERROR: "
            . xml_error_string(xml_get_error_code($xmlparser))
            . "<br />"
            . "Line: "
            . xml_get_current_line_number($xmlparser)
            . "<br />"
            . "Column: "
            . xml_get_current_column_number($xmlparser)
            . "<br />"
            . "Byte Index: "
            . xml_get_current_byte_index($xmlparser)
            . "<br />");
       }
    }

    xml_parser_free($xmlparser);
?>

The following code is for test.xml.


    <?xml version="1.0" encoding="UTF-8"?>
    <note>//ww  w  .  j  a  v  a 2 s.co m
    <to>Work</to>
    <from>Home</from>
    <heading>Reminder</heading>
    <body>This is a message.</body>
    </note>




















Home »
  PHP Tutorial »
    XML »




DOM
SimpleXML
SimpleXMLElement
XML Parser