Tuesday 25 March 2008

Validate XML in IE using JavaScript

Slightly off-topic, but as there don’t seem to be many good examples on the web of how to validate an XML document in IE using JavaScript, here’s a quick snippet. In the event of an XML error, an error message is displayed in the div.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>XML Validator</title>

<script type="text/javascript">
function loadit()
{
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.validateOnParse = true;
xmlDoc.async="false";
xmlDoc.load("MyXmlFile.xml");

if (xmlDoc.parseError != '0')
{
document.getElementById("error").innerHTML
= xmlDoc.parseError.reason;
}

}
</script>

</head>
<body onload="loadit();">
<div id="error">
</div>
</body>
</html>

No comments: