Magazine |
| | Community |
| | Workshop |
| | Tools & Samples |
| | Training |
| | Site Info |
|
|
||||||||
|
Microsoft Corporation
January 7, 1998
Contents
Introduction
XML Document Properties and Methods
XML Element Properties and Methods
Element Collection Properties and Methods
Sample JScript Program
The XML object model in Microsoft® Internet Explorer 4.0 provides a means by which you can navigate and manipulate an XML document. An XML document is a tree-like structure, starting with the top-level element (its root) and branching out to its descendants. Being able to navigate this tree enables you to retrieve important information concerning the source document.
This document explains how to access the XML object model from script in Internet Explorer 4.0. Information on the C++ interfaces can be found in the Internet Client SDK (see the
XML Object Model Reference section). At the end of this document, you'll find an example of a JScript program that uses the XML object model to display an XML document in a Web page.
In accessing the XML object model, three objects are used: the XML Document, the XML Element, and the Element Collection.
Figure 1. Accessing the XML object model
The XML Document is an object representing an XML source document. That Document consists of a root element (the top-level element) and its descendants (the other elements). An Element Collection is a group of sibling elements.
You create an XML document object by creating a new ActiveXObject:
var xml = new ActiveXObject("msxml");
The above code assigns the XML Document object to the variable xml.
Once you have created an XML Document object, you can access information concerning the object and manipulate the object by calling the following properties and methods. In the examples, xml is the document created above.
xml.URL = "http://Chein/_private/pdcxml.xml";
xml.URL = "http://Chein1/_private/newXML.xml";
var docroot = xml.root;
var newElem = xml.createElement(0, "NEW-DESCRIPTION");
With access to these properties and methods, you can now specify the XML data file to be loaded by the XML Document object:
xml.URL = "http://Chein1/_private/pdcxml.xml7quot;;
Likewise, using root you can now identify the document root of the XML document pointed to by the URL specified above:
var docRoot = xml.root
Having located the document root, it becomes easy to navigate the tree. The following properties and methods allow you access to the document root and all of its children. In the examples, elem represents an XML Element object.
0 - ELEMENTFor Internet Explorer 4.0, the interesting element types are ELEMENT, TEXT, and COMMENT. In the JScript program at the end of this document, elem.type is used to differentiate between tag names and text.
1 - TEXT
2 - COMMENT
3 - DOCUMENT
4 - DTD
if (elem.type == 0) return "ELEMENT";
if (elem.type == 0) //if it is a tag return elem.tagName; //return the tag name as a string
if (elem.tagName == "PAINTING") //if aelement elem.tagName = "OIL-PAINTING"; //change its tag name to "OIL-PAINTING"
if (elem.type == 1) //if the node is a text node return elem.text;
if (elem.type == 1) elem.text = "This is an element"; //replaces the text of the element //with the string "This is an element"
if (elem.children.item(0) != null) //if the element has at least one child elem.addChild(elem.children.item(0).children.item(0), 0, -1); //add a descendant as a child
if (elem.children.item(0).length > 1) //if the element has more than one child elem.removeChild(elem.children.item(1)); //remove the second child element
if (elem != xml.root) //if the element is not the root element return elem.parent.tagName;
if (elem.getAttribute("font-style") == "italic") return "italic";
if (elem.getAttribute("font-style") == "italic") elem.setAttribute("font-style","normal");
if (elem.getAttribute("font-style") == "italic") elem.removeAttribute("font-style");
if (elem.children != null) // if the element has children return "yes";
The children of an element are manipulated as Element Collection objects. You can access the members of a collection by index and by tag name. If children are accessed by tag name and two or more children share that name, a specialized collection is created. The members of such a specialized collection can be accessed by index, just like the members of a general collection.
The item() method can be called to access and manipulate members of an Element Collection. The length property can be accessed to retrieve information concerning the length of an Element Collection.
return elem.children.item(0).text; //return the text of the first child element return elem.children.item("TITLE") //return a collection of <TITLE> elements return elem.children.item("TITLE",3).text //return the text of the fourth <TITLE> element
for (i = 0; i < elem.children.length; i++) output_doc(elem.children.item(i),(indents + 1));
Let's now take a look at a JScript program that uses the object model to reveal the contents of an XML document.
The script first creates an ActiveXObject representing an XML document and then passes the root of that document to output_doc(). This function then inserts the root element's tag name and contents into the HTML element with an ID value of "results," repeating the process for each element within the source document. The program results in the XML document being displayed in the HTML page.
Note: The inability to enumerate attributes precludes the display of any attributes an element in the source document might have.
<SCRIPT LANGUAGE="JScript" FOR=window EVENT=onload> var indent_array = new String(" "); var str = ""; var xml = new ActiveXObject("msxml"); xml.URL = "http://Chein/_private/pdcxml.xml"; var docroot = xml.root; output_doc(docroot,0); function output_doc(elem,indents) { var i; if (elem.type == 0) // 0 is a tagName { document.all("results").insertAdjacentText("BeforeEnd", indent_array.substring(0,(4 * indents)) + "<" + elem.tagName + ">" + "\n"); if (elem.children != null) { for (i = 0 ; i < elem.children.length ; i++) output_doc(elem.children.item(i),(indents + 1)); } document.all("results").insertAdjacentText("BeforeEnd", indent_array.substring(0,(4 * indents)) + "</" + elem.tagName + ">" + "\n"); } else if (elem.type == 1) // 1 is a text node { document.all("results").insertAdjacentText("BeforeEnd", indent_array.substring(0,(4 * indents)) + "\"" + elem.text + "\"\n"); } else alert("unknown element type: " + elem.type); } </script>
As you can see from the above script, the XML object model in Internet Explorer 4.0 makes it quite easy to write an application that can traverse an XML document.
Did you find this article useful? Gripes? Compliments? Suggestions for other articles? Write us!
© 1998 Microsoft Corporation. All rights reserved. Terms of use.