Validating XML Documents with DTDs in Internet Explorer

By default, Internet Explorer actually does validate XML documents with DTDs as it loads them, but you won't see any validation errors unless you check the parseError object.

Turning Validation On and Off

You 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, ch07_10.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:

Listing ch07_10.xml
 <?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, 2003</DATE>         <ORDERS>             <ITEM>                 <PRODUCT>Tomatoes</PRODUCT>                 <NUMBER>8</NUMBER>                 <PRICE>.25</PRICE>             </ITEM>             <ITEM>                 <PRODUCT>Asparagus</PRODUCT>                 <NUMBER>12</NUMBER>                 <PRICE>.95</PRICE>             </ITEM>             <ITEM>                 <PRODUCT>Lettuce</PRODUCT>                 <NUMBER>6</NUMBER>                 <PRICE>.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:

Listing ch07_11.html
 <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('ch07_10.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, "&lt;")                     errorString = errorString.replace(/\>/g, "&gt;")                     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> 

You can see the results of this Web page in Figure 7-8, where the validation error is reported .

Figure 7-8. Validating XML documents in Internet Explorer.

graphics/07fig08.gif

Replace < with &lt; and > with &gt;

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 &lt; and > with &gt; . (You pass a regular expression to the replace method.) To change all < characters to &lt; , you use the regular expression /\</g; . To change all > characters to &gt; , you match to the regular expression /\>/g .



Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net