Click to return to the XML (Extensible Markup Language) home page    
Lesson 3     Lesson 5     Lesson List    
Web Workshop  |  XML (Extensible Markup Language)

XML Tutorial
Lesson 4: Using the XML Object Model


What is the XML object model?

The XML object model is a collection of objects that you use to access and manipulate the data stored in an XML document. The XML document is modeled after a tree, in which each element in the tree is considered a node. Objects with various properties and methods represent the tree and its nodes. Each node contains the actual data in the document.

How do I access the nodes in the tree?

You access nodes in the tree by scripting against their objects. These objects are created by the XML parser when it loads and parses the XML document. You reference the tree, or document object, by its ID value. In the following example, MyXMLDocument is the document object's ID value. The document object's properties and methods give you access to the root and child node objects of the tree. The root, or document element, is the top-level node from which its child nodes branch out to form the XML tree. The root node can only appear in the document once.

Run the mouse over the following data island to reveal the code needed to access each node. The root node is <class>, and its child node is <student>, which has child nodes of <name> and <GPA>.

<XML ID="MyXMLDocument">
  <class>
    <student studentID="13429">
      <name>Jane Smith</name>
      <GPA>3.8</GPA>
    </student>
  </class>
</XML>
The following list is a sample of the properties and methods that you use to access nodes in an XML document.

An HTML page containing an XML data island is shown below. The data island is contained within the <XML> element.

<HTML>
  <HEAD>
    <TITLE>HTML with XML Data Island</TITLE>
  </HEAD>
  <BODY>
    <P>Within this document is an XML data island.</P>
    <XML ID="resortXML">
      <resorts>
        <resort>Calinda Cabo Baja</resort>
        <resort>Na Balam Resort</resort>
      </resorts>
    </XML>
  </BODY>
</HTML>

Calinda Cabo Baja Na Balam Resort

You access the data island through the ID value, "resortXML", which becomes the name of the document object. In the example above, the root node is <resorts>, and the child nodes are <resort>.

The following code accesses the second child node of <resorts> and returns its text, "Na Balam Resort."

resortXML.XMLDocument.documentElement.childNodes.item(1).text

How do I persist XML DOM tree information?

Several methods and interfaces are available for persisting DOM information.

If you're using a script language, the XMLDOMDocument object exposes the load, loadXML, and save methods, and the xml property.

For Visual Basice and C or C++ programmers, the IXMLDOMDocument interface exposes the same members as the XMLDOMDocument object. IXMLDOMDocument also implements standard COM interfaces such as IPersistStreamInit, IPersistMoniker, and IStream.

Try it!

In the text box below, enter code to access a part of either of the above documents (assume that the first document object is "MyXMLDocument" and the second is "resortXML"). Then click the Access XML button to reveal the node you have referenced.

Jane Smith 3.8


Back to topBack to top

Did you find this material useful? Gripes? Compliments? Suggestions for other articles? Write us!

© 1999 Microsoft Corporation. All rights reserved. Terms of use.