3

I have an XML doc that I need to load with PHP. I am currently using the simplexml_load_file() function, however the xml file is malformed, and consequently I am getting a parse error.

The XML file looks something like this:

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

As you can see, this XML is whack and this function is throwing an error trying to parse it. Also I don't need this data that is corrupted. I would just like to read in the stuff that I can and throw everything else away.

3

3 Answers 3

2

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

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

$dom->loadHTML($xml);
1

@Juliusz

You don't actually need to set the strictErrorChecking for this I don't think. I tried the following and it seems to work fine. To ignore the errors you need to set the libxml_use_internal_errors(true). Essentially you want to use DOMDocument instead of simplexml. I tried the following and worked without any problems:

<?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();


?>

Thusjanthan Kubendranathan

0

try to tidy it up, it worked well for me.

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.