Get the XML parser error description in PHP

Description

The following code shows how to get the XML parser error description.

Example


/*from  w  ww  .  j ava 2  s  .c om*/
<?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 />");
      }
    }

    xml_parser_free($xmlparser);
?>

The following code is for test.xml.


    <?xml version="1.0" encoding="UTF-8"?>
    <note>/* w  w  w  .  j  a  v a2s . c  o 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