Jump to content

Need help with XML validation


mpsn

Recommended Posts

Hi, I have a mistmatched tag <messagesss></message> BUT it still displays "Validated XML!" BUT then proceeds to the else that outputs each XML error!

 

here is the XML:

<?xml version="1.0" encoding="utf-8"?>
<email>
<messagesss>
	<to>
		<toFirstName>Tove</toFirstName>
		<toLastName toType="common" style="swag">Smith</toLastName>
	</to>
	<from><fromdd/>
		<fromFirstName>Jani</fromFirstName>
		<fromLastName fromType="unique">Dravison</fromLastName>
	</from>
</message>
</email>

 

<?php
$dom=new DOMDocument();
$dom->load("emailSimple.xml");
$isValidated=false;
$dom->formatOutput = true;
$dom->saveXML();
$errors=libxml_get_errors();//Returns array where each XML file line is an elem
if(!file_exists("emailSimple.xml"))
print "no such file!";
else if(strlen(file_get_contents("emailSimple.xml"))==0)
print "File is empty!";
else if($dom) {//IF file exists and has content
if(empty($errors))
	print "Validated XML!";//isValidated=true so now shred!
else
{
	//CHECK if current XML file is Well-formed
	foreach($errors AS $error) {//FOR EACH ERROR OF CURRENT XML FILE TO CHECK
		echo "Error Code: ".$error->code."<br />";
		echo "Error message: ".$error->message;

		//Column is the end of the line where error is
		echo "line".$error->line.", column".$error->column."<br />";

		echo "----------------------------------------------<br /><br />";
	}
}
libxml_clear_errors();
}
?>

Link to comment
https://forums.phpfreaks.com/topic/251644-need-help-with-xml-validation/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.