Before we take a look at the XML Document Object Model (DOM), let's talk just a little bit about XML processors in general and Microsoft's XML processor in particular. In principle, an XML processor has three main tasks:
Microsoft Internet Explorer has a built-in XML processor. It's in a system file named msxml.dll. You can use it in Web pages that accept ActiveX controls, as we did in the HTML and JavaScript example earlier in this chapter, but you can also use it in any program written with a language that supports ActiveX. Naturally, Microsoft Visual Basic and Visual C++ are two examples of such languages.
Figure 20-10 is a representation of an XML document loaded in Microsoft's XML DOM. The gray circle at the top of the diagram represents the document itself. The top node of the diagram is the root node; under the root node you find the content of the document as objects of different classes.
Figure 20-10. Example of an XML Document object.
Right under the root object is one Comment object, potentially containing comments that pertain to the entire document. On the same level, you'll find two Customer objects. One of the Customer objects is expanded, showing two child nodes. The first of the child nodes is a Name object, which in turn has two child nodes. The first of these child nodes represents the first name and has the text value Jane; the second one represents the last name, and its text value is Doe. All these objects are created from the Node class.
The Customer object also has a child node not created from the Node class but from the NodeList class. That object is an order list, and it contains two Order objects.
These examples show that XML elements can be expanded by the use of other elements; for example, the Horserace element in the sample we've seen quite a few times now is expanded to include a Track element, a Date element, a Raceno element, and a Distance element. What we haven't seen so far is that it's also possible to expand an XML element by using attributes. Instead of containing the elements just enumerated, the Horserace element could have been specified by the use of a corresponding list of attributes: a track attribute, a date attribute, and so on. The choice is up to you when you define a document type such as the Horserace type. Whether you use subelements or attributes, the XML DOM can represent them for you and make the details of the document available to applications. This is what you can see in Figure 20-10, in which some of the elements shown also possess an attribute.
In an earlier section of this chapter, "Showing the Document on a Web Page," you saw an example of a script reading XML elements out of the XML DOM. One part of the script loaded the document into the XML DOM object; another part read the text values of each separate text element out of the DOM, assigning each text element to the innerText property of an HTML SPAN element. For your convenience, we show the JavaScript code again here:
<SCRIPT LANGUAGE="JavaScript" FOR=window EVENT=onload> loadXMLDoc(); </SCRIPT> <SCRIPT LANGUAGE="JavaScript"> var theRoot; var xmlDoc = new ActiveXObject("microsoft.xmldom"); xmlDoc.load("HorseraceDTD.xml"); function loadXMLDoc() { if (xmlDoc.readyState == "4") start(); else window.setTimeout("loadDoc()", 4000) } function start() { theRoot=xmlDoc.documentElement; spanTrack.innerText=theRoot.childNodes.item(0).text; spanDate.innerText=theRoot.childNodes.item(1).text; spanRace.innerText=theRoot.childNodes.item(2).text; spanDistance.innerText=theRoot.childNodes.item(3).text; } </SCRIPT> |