spacer
Yehuda Shiran February 25, 2002
Validating DOMDocument Upon Request
Tips: February 2002

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

Since you can add and modify nodes to the DOMDocument tree, there must be a way to validate the DOMDocument upon request, and not only upon loading. You can do this in later versions of Internet Explorer with the validate() method. Here is code that loads an XML file, adds an attribute to the root node, and validates the new DOMDocument:

function validateXML() {
  var xmlDoc = new ActiveXObject("Msxml2.DOMDocument");
  xmlDoc.async = false;
  xmlDoc.load("mydvd7.xml");
  xmlDoc.documentElement.setAttribute("foo", "bar");
  var err = xmlDoc.validate();
  if (err.errorCode == 0) {
    alert("Document is valid");
  } else {
      alert("Validation error:" + err.reason);
	}
}
Notice the err error object that holds very useful information. For example, it pinpoints the fact that the new attribute "foo" is not defined in the DTD file and hence is illegal. Try it.


People who read this tip also read these tips:

Look for similar tips by subject:

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business