By default, Internet Explorer actually does validate XML documents as it loads them, but you won't see any validation errors unless you check the parseError object.
Turning Validation On and OffYou can turn document validation on or off with the document object's validateOnParse property, which is set to true by default. |
Here's an example; in this case, I'll load this XML document, error.xml. This document has a validation problem because the <NAME> element is declared to contain only a <FIRST_NAME> element, not a <LAST_NAME> element:
<?xml version = "1.0" standalone="yes"?> <!DOCTYPE DOCUMENT [ <!ELEMENT DOCUMENT (CUSTOMER)*> <!ELEMENT CUSTOMER (NAME,DATE,ORDERS)> <!ELEMENT NAME (FIRST_NAME)> <!ELEMENT LAST_NAME (#PCDATA)> <!ELEMENT FIRST_NAME (#PCDATA)> <!ELEMENT DATE (#PCDATA)> <!ELEMENT ORDERS (ITEM)*> <!ELEMENT ITEM (PRODUCT,NUMBER,PRICE)> <!ELEMENT PRODUCT (#PCDATA)> <!ELEMENT NUMBER (#PCDATA)> <!ELEMENT PRICE (#PCDATA)> ]> <DOCUMENT> <CUSTOMER> <NAME> <LAST_NAME>Smith</LAST_NAME> <FIRST_NAME>Sam</FIRST_NAME> </NAME> <DATE>October 15, 2001</DATE> <ORDERS> <ITEM> <PRODUCT>Tomatoes</PRODUCT> <NUMBER>8</NUMBER> <PRICE>$1.25</PRICE> </ITEM> <ITEM> <PRODUCT>Asparagus</PRODUCT> <NUMBER>12</NUMBER> <PRICE>$2.95</PRICE> </ITEM> <ITEM> <PRODUCT>Lettuce</PRODUCT> <NUMBER>6</NUMBER> <PRICE>$11.50</PRICE> </ITEM> </ORDERS> </CUSTOMER> </DOCUMENT>
Here's what the Web page that reads in and checks this document looks like—here I'm using the parseError object's errorCode, url, line, linepos, errorString, and reason properties to track down the error:
<HTML> <HEAD> <TITLE> Validating documents </TITLE> <SCRIPT LANGUAGE="JavaScript"> var xmldoc function loadDocument() { xmldoc = new ActiveXObject("microsoft.XMLDOM") xmldoc.onreadystatechange = stateChangeHandler xmldoc.ondataavailable = dataAvailableHandler xmldoc.load('error.xml') } function dataAvailableHandler() { messageDIV.innerHTML += "Status: data available.<BR>" } function stateChangeHandler() { if(xmldoc.readyState == 4){ var errorString = xmldoc.parseError.srcText errorString = xmldoc.parseError.srcText.replace(/</g, "<") errorString = errorString.replace(/>/g, ">") if (xmldoc.parseError.errorCode != 0) { messageDIV.innerHTML = "Problem in " + xmldoc.parseError.url + " line " + xmldoc.parseError.line + " position " + xmldoc.parseError.linepos + ":<BR>Error source: " + errorString + "<BR>" + xmldoc.parseError.reason + "<BR>" + "Error: " + xmldoc.parseError.errorCode } else { messageDIV.innerHTML = "Status: document loaded alright.<BR>" } } } </SCRIPT> </HEAD> <BODY> <CENTER> <H1> Validating documents </H1> </CENTER> <DIV ID="messageDIV"></DIV> <CENTER> <INPUT TYPE="BUTTON" VALUE="Load the document" ONCLICK="loadDocument()"> </CENTER> </BODY> </HTML>
Figure 7.9 shows the results of this Web page, where the validation error is reported.
You might note that the errorString property holds the error-causing text from the XML document. Because that text is <LAST_NAME>Smith</LAST_NAME>, there's a problem—the browser will try to interpret this as markup. To avoid that, I use the JavaScript String object's replace method to replace < with < and > with >. (You pass a regular expression to the replace method; to change all < characters to <, the regular expression that you use is /</g. To change all > characters to >, you match to the regular expression />/g.)
18.220.65.61