0

To read XML files use:

$XMLFile = new XMLReader();

if($XMLFile->open('file.xml') === TRUE){                            

    while($XMLFile->read()) {

        //Do something

    }

    $XMLFile->close();

}

If I will find in xml file sample string:

!+_)(*&^%$#@!~}|"?,../;'\[]=-

Critical error is displayed and the parsing is terminated:

Warning: XMLReader::read() [xmlreader.read]: file.xml:16: parser error : xmlParseEntityRef: no name in test.php on line 841
Warning: XMLReader::read() [xmlreader.read]: An Error Occured while reading in test.php on line 841

In this case, I would like to handle the error and delete the xml file. Someone may know how to fix this error?

Kuba
  • 147
  • 1
  • 3
  • 14

3 Answers3

2

There are many different ways to deal with the error-condition you have there. But first of all I think you should know that XMLReader is based on libxml and libxml offers various functions and even the LibXMLError object for the error-handling:

$reader = new XMLReader();
if (!$reader->open($file)) {
    throw new RuntimeException('Unable to open file.');
}

while ($reader->read()) {
    //Do something
}

if (libxml_get_last_error()) {
    // There was an error reading the file
    unlink($file);
}

The error information exemplary is:

LibXMLError Object
(
    [level] => 3
    [code] => 68
    [column] => 12
    [message] => xmlParseEntityRef: no name

    [file] => /path/to/file.xml
    [line] => 2
)

For this exemplary XML file:

<root>
    !+_)(*&^%$#@!~}|"?,../;'\[]=-
</root>

If you want to reduce the error output, you can make use of something called internal errors in libxml.

See as well:

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
0

I was having the same issue. The problem is that your XML is invalid. You are not supposed to have special chars in XML (i.e. & should be converted to & during XML file generation).

For more info, you can see this similar problem.

You basically have two options:

  1. You can fix the file before parsing (for example using regex)
  2. You can fix the code that generates XML in the first place
Community
  • 1
  • 1
Dejv
  • 944
  • 2
  • 14
  • 31
-2

Use XMLReader::isValid().

<?php $xml = XMLReader->open('file.xml');

// You have to use this:
$xml->setParserProperty(XMLReader::VALIDATE, true);

var_dump($xml->isValid());
?>
Shi
  • 4,178
  • 1
  • 26
  • 31
VNTeck
  • 32
  • 1
  • now i have new error: validity error : Validation failed: no DTD found ! – Kuba Jun 08 '13 at 12:53
  • For XML, there is "valid" and "well-formed". For well-formed, the general syntax must be correct (matching tags, proper formatting). For valid, it must be well-formed and must adhere to the DTD. Without DTD, it can only be well-formed, but never valid. – Shi Oct 30 '15 at 02:24
  • The other comments aside, `XMLReader->open` is not valid. You need to create an instance of `XMLReader` first (in case anyone else was hoping this might have been shorthand for opening a XML file). – toast Mar 17 '19 at 23:27