Page 1 of 1

How to fix errors in XML file using php program?

Posted: Tue Dec 21, 2010 10:57 pm
by hmdnawaz
<?xml version="1.0" encoding="ISO-8859-1"?>

<Note>
<from>
Ahmad
</from>
<to>
Yasir
</to>
<body>
hello yasir.

</Note>


Above is my xml file which misses the ending tag of <body>.
i want a php program that will detect this error and also fix the error and place the ending tag.
can someone help me?

Re: How to fix errors in XML file using php program?

Posted: Wed Dec 22, 2010 2:56 am
by dejan
hmdnawaz wrote: Above is my xml file which misses the ending tag of <body>.
i want a php program that will detect this error and also fix the error and place the ending tag.
can someone help me?
You can read about this in some detail here: http://www.devnull.rs/php/blog/parsing- ... -with-php/

Essentially, an easy way to do it is:

Code: Select all

$doc = new DOMDocument();
$doc->loadXML($invalid_xml);
$valid_xml = $doc->saveXML();
If that's HTML and not XML, you'd use loadHTML instead of loadXML. Keep in mind, it will still complain about errors, but it will parse it (best to it's ability to parse invalid XML).