Beginning XML Databases (Wrox Beginning Guides)

In this section you read about and experiment with non-structural and somewhat non-central XML DOM classes. Lets begin with the parseError class.

The parseError Class

The parseError class is exclusive to Microsoft and Internet Explorer. It can be used to get details of what an error is, should one have occurred, as an XML document is loaded and parsed in the browser. Figure 2-16 shows all the interesting attributes and methods for the parseError class.

Figure 2-16: The XML DOM parseError class can contain error details.

Here is an example XML document containing a very simple error. The start element <World> does not match the ending element <world> because the case of the two element names is different:

<?xml version="1.0"?> <WORLD> <countries> <country> <name>Canada</name> <states> <state> <name>Quebec</name> <cities> <city> <name>Montreal</name> <type>French</type> </city> </cities> </state> <state> <name>British Columbia</name> <cities> <city> <name>Vancouver</name> <type>Nice</type> </city> </cities> </state> </states> </country> <country> <name>United States</name> <states> <state> <name>New York</name> <cities> <city> <name>New York City</name> <type>Commercial</type> </city> <city> <name>Buffalo</name> <type>Big Snow Balls</type> </city> </cities> </state> <state> <name>California</name> <cities> <city> <name>San Francisco</name> <type>Silicon Valley</type> </city> <city> <name>Los Angeles</name> <type>Polluted</type> </city> <city> <name>San Diego</name> <type>Sunny</type> </city> </cities> </state> </states> </country> </countries> </world>

In order to access information placed into a parseError object by the most recent error produced, the parseError class can be as in the following HTML and JavaScript code:

<HTML><BODY> <SCRIPT LANGUAGE="JavaScript"> var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async = false; xmlDoc.load("cities.xml"); document.write(xmlDoc.xml); if (xmlDoc.parseError.errorCode != 0) { document.writeln("Error " + xmlDoc.parseError.errorCode + " on line " + xmlDoc.parseError.line + ", position " + xmlDoc.parseError.linepos + "<BR><BR>"); document.writeln(xmlDoc.parseError.reason + "<BR><BR>"); document.writeln(xmlDoc.parseError.srcText + "<BR><BR>"); } </SCRIPT> </BODY></HTML>

Setting the preceding async attribute to false ensures asynchronous download of the XML document. The entire document is parsed prior to executing the rest of this script.

The result is as shown in Figure 2-17 where in the first browser window (top-left of the diagram) the error is present in the cities.xml file, as shown previously. The second browser window (bottom-right of the diagram) has the error corrected, with both root elements set as <world> .

XML is case sensitive!

Figure 2-17: Using the parseError class

HTTPRequest Class

An HTTP request allows a web page to be changed without the page being reloaded in the clients browser. Attributes and methods for the HTTPRequest object are shown in Figure 2-18.

Figure 2-18: The XML DOM HTTPRequest class passes data between XML documents and the server.

This following example tests for a URL to see if it exists:

<HTML><BODY> <SCRIPT LANGUAGE="JavaScript"> var url = "http://www.oracledbaexpert.com/xml/parts.xml"; var xmlHTTP = new ActiveXObject("Microsoft.xmlHTTP"); xmlHTTP.open("HEAD", url, true); xmlHTTP.onreadystatechange = function() { if (xmlHTTP.readyState == 4) { if (xmlHTTP.status == 200) { alert (url + " OK"); } else if (xmlHTTP.status == 404) { alert (url + " not found"); } else { alert (xmlHTTP.status + ": " + xmlHTTP.statusText); } } } xmlHTTP.send(); </SCRIPT> </BODY></HTML>

The parts.xml page does exist on my website at that URL address, as shown in the top left of the diagram in Figure 2-19. In the bottom right of the diagram in Figure 2-19, I set the URL variable in the preceding script to a URL that does not exist.

Figure 2-19: Using the HTTPRequest class

Other Classes

Various other XML DOM classes can sometimes be useful:

The ProcessingInstruction class is required when building an XML document from scratch.

Категории