Share via


Use the XML Object Model

 

This topic discusses 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 appear in the document only once.

The following is a data island. 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>James 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.

Property/Method Description
XMLDocument Returns a reference to the XML Document Object Model (DOM) exposed by the object.
documentElement Returns the document root of the XML document.
childNodes Returns a node list containing the children of a node (if any).
item Accesses individual nodes within the list through an index. Index values are zero-based, so item(0) returns the first child node.
text Returns the text content of the node.

The following code shows an HTML page containing an XML data island. 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>Adventure Works</resort>
        <resort>Alpine Ski House</resort>
      </resorts>
    </XML>
  </BODY>
</HTML>

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

The following Jscript code accesses the second child node of <resorts> and returns its text, Alpine Ski House.

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 are using a script language, the DOMDocument object exposes the load, loadXML, and save methods, and the xml property.

For Microsoft® Visual Basic® and C or C++ programmers, the IXMLDOMDocument interface exposes the same members as the DOMDocument object. The IXMLDOMDocument interface also implements standard COM interfaces such as IPersistStreamInit, IPersistMoniker, and IStream.

See Also

IXMLDOMDocument-DOMDocument