The Microsoft XML DOM
The Document Object Model (DOM)
 | The Microsoft DOM is a programming interface for HTML and XML documents. |
 | It defines a
way that XML documents can be accessed and manipulated from various software
environments.. |
 | Using a DOM, a programmer can create a document, navigate its structure, and
add, modify, or delete its elements. |
 | As a W3C specification, one important objective for the DOM has been to
provide a standard programming interface that can be used in a wide variety of
environments and applications. |
 | The W3C DOM has been
designed to be used with any programming language. |
The Node Interface
 | When the
document is loaded, it's information can be retrieved and manipulated by
accessing the Document Object Model (DOM). |
 | The DOM represents a tree view of the XML document. |
 | The documentElement
is the top-level of the tree. This element has one or many childNodes
that represent the branches of the tree. |
 | A Node Interface is used to read and write the
individual elements in the XML node tree. |
 | The childNodes property of the
documentElement can be accesses with a for/each construct to enumerate each
individual node. |
 | The Microsoft XML parser supports all the necessary functions to traverse the node tree, access the nodes
and their attribute values, insert and delete nodes, and convert the node tree
back to XML. |
 | A total of 13 node types are currently supported by the Microsoft XML
parser. |
 | The following table lists the most commonly used node types: |
| Node Type |
Example |
| Document type |
<!DOCTYPE food SYSTEM "food.dtd"> |
| Processing instruction |
<?xml version="1.0"?> |
| Element |
<drink type="soda">Pepsi</drink> |
| Attribute |
type="soda" |
| Text |
Pepsi |

Parsing the DOM
Using the XML parser
 | To read and update - create and manipulate - an XML document, you need an XML
parser. |
 | The Microsoft XML parser is a COM component that comes with Microsoft
Internet Explorer 5.0/6.0 (future references will assume the use of a 5.0 or 6.0
IE browser). |
 | Once you have installed IE, the parser is available
to scripts inside HTML documents and ASP files. |
 | The Microsoft XMLDOM parser features a language-neutral programming model
that:
 | Supports JavaScript, VBScript, Perl, VB, Java, C++ and more |
 | Supports W3C XML 1.0 and XML DOM |
 | Supports DTD and validation |
|
 | If you are using JavaScript in IE, you can create an XML document object
with the following code: |
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
 | If you are using VBScript you create the XML document object with the
following code: |
set xmlDoc = CreateObject("Microsoft.XMLDOM")
 | If you are using VBScript in an Active Server Page (ASP), you can use the
following code: |
set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")
Loading an XML file into the parser
 | The following code loads an existing XML document (note.xml) into the XML
parser: |
<script language="JavaScript">
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("note.xml")
// ....... processing the document goes here
</script>
 | The first line of the script creates an instance of the Microsoft XML parser. |
 | The third line tells the parser to load an XML document called note.xml. |
 | The
second line assures that the parser will halt execution until the document is
fully loaded. |
Loading pure XML text into the parser
 | The following code loads a text string into the XML parser: |
<script language="JavaScript">
var text="<letter>"
text=text+"<to>Bob</to><from>John</from>"
text=text+"<subject>Reminder</subject>"
text=text+"<body>See me tomorrow...</body>"
text=text+"</subject>"
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.loadXML(text)
// ....... processing the document goes here
</script>
 | Note that the "loadXML" method (instead of the "load"
method) is used to load a text string. |

Parser Errors
The parseError Object
 | If you try to open an XML document, the XML Parser might generate an error. |
 | By accessing the parseError object, the exact error code, the error text, and
even the line that caused the error can be retrieved: |
File Error
 | In this example we let the XML parser try to load a non existing file, and
display some of its error properties: |
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("ksdjf.xml")
document.write("<br>Error Code: ")
document.write(xmlDoc.parseError.errorCode)
document.write("<br>Error Reason: ")
document.write(xmlDoc.parseError.reason)
document.write("<br>Error Line: ")
document.write(xmlDoc.parseError.line)
XML Error
 | Now, let the parser load an XML document that is not well formed. |
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("note_error.xml")
document.write("<br>Error Code: ")
document.write(xmlDoc.parseError.errorCode)
document.write("<br>Error Reason: ")
document.write(xmlDoc.parseError.reason)
document.write("<br>Error Line: ")
document.write(xmlDoc.parseError.line)
The parseError Properties
| Property |
Description |
| errorCode |
Returns a long integer error code |
| reason |
Returns a string explaining the reason for the error |
| line |
Returns a long integer representing the line number for the error |
| linePos |
Returns a long integer representing the line position for the error |
| srcText |
Returns a string containing the line that caused the error |
| url |
Returns the url pointing the loaded document |
| filePos |
Returns a long integer file position of the error |

Accessing the DOM
Traversing the node tree
 | One very common way to extract XML elements from an XML document is to
traverse the node tree and extract the text value of each elements. |
 | A small
snippet of programming code like a VBScript for/each construct can be written to
demonstrate this. |
 | The following VBScript code traverses an XML node tree, and displays the XML
elements in the browser: |
set xmlDoc=CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("note.xml")
for each x in xmlDoc.documentElement.childNodes
document.write(x.nodename)
document.write(": ")
document.write(x.text)
next
Providing HTML content from XML files
 | By using an XML parser inside the browser, an HTML
page can be constructed as a static document, with an embedded JavaScript to
provide dynamic data. |
 | When you add that these JavaScripts can access Active
Server Pages from a Web server, the future looks very bright. |
 | The following JavaScript reads XML data from an XML document and writes the
XML data into (waiting) HTML elements. |
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("note.xml")
nodes = xmlDoc.documentElement.childNodes
to.innerText = nodes.item(0).text
from.innerText = nodes.item(1).text
header.innerText = nodes.item(2).text
body.innerText = nodes.item(3).text
Accessing XML elements by name
 | The following JavaScript reads XML data from an XML document and writes the
XML data into (waiting) HTML elements. |
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("note.xml")
document.write(xmlDoc.getElementsByTagName("from").item(0).text)
|