Loading invalid XML in PHP

I have an XML document that I need to upload using PHP. I am currently using the simplexml_load_file () function, however the xml file is incorrect, and therefore I am getting a parsing error.

The XML file looks something like this:

...
</result>something1>
</else>
</else>
</resu
...

As you can see, this XML is hacked and this function throws an error trying to parse it. Also I do not need this data that is damaged. I just wanted to read everything I can and throw away the rest.

+3
source share
3 answers

As Jon Bron suggested, try DOMDocument :: loadHTML ():

$dom = new DOMDocument();
$dom->strictErrorChecking = false;
libxml_use_internal_errors(true);

$dom->loadHTML($xml);
+2
source

@Juliusz

strictErrorChecking , . , , . , libxml_use_internal_errors (true). , DOMDocument simplexml. :

<?php

$string = <<<XML
<?xml version='1.0'?>
<document>
    <cmd>login</cmd>
    <login>Richard</login>

</else>
</else>
</document>
XML;

$dom = new DOMDocument();
libxml_use_internal_errors(true); 
$dom->loadHTML($string);
print $dom->saveHTML();


?>

,

+1

try to remove it, it worked for me.

http://hu2.php.net/manual/en/intro.tidy.php

0
source

Source: https://habr.com/ru/post/1781757/


All Articles