How to fix errors in XML file using php program?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hmdnawaz
Forum Newbie
Posts: 14
Joined: Tue Dec 21, 2010 10:55 pm

How to fix errors in XML file using php program?

Post 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?
dejan
Forum Newbie
Posts: 8
Joined: Tue Dec 21, 2010 6:11 am

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

Post 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).
Post Reply