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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.