ThePlace

Home ] Search ] Resources ] Site Map ] Contact Me ]
Dave's Information Technology Resource

Up ]

What is XML ] Viewing XML ] XML Syntax ] Creating XML ] XML and CSS ] XML and DTD ] XML, XSL, and HTML ] [ The XML DOM ]

--- The XML DOM ---

The Microsoft XML DOM

The Document Object Model (DOM)

bulletThe Microsoft DOM is a programming interface for HTML and XML documents. 
bulletIt defines a way that XML documents can be accessed and manipulated from various software environments.
bulletUsing a DOM, a programmer can create a document, navigate its structure, and add, modify, or delete its elements.
bulletAs 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.
bulletThe W3C DOM has been designed to be used with any programming language.

The Node Interface

bulletWhen the document is loaded, it's information can be retrieved and manipulated by accessing the Document Object Model (DOM).
bulletThe DOM represents a tree view of the XML document. 
bulletThe documentElement is the top-level of the tree. This element has one or many childNodes that represent the branches of the tree.
bulletA Node Interface is used to read and write the individual elements in the XML node tree. 
bulletThe childNodes property of the documentElement can be accesses with a for/each construct to enumerate each individual node.
bulletThe 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.
bulletA total of 13 node types are currently supported by the Microsoft XML parser
bulletThe 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

bulletTo read and update - create and manipulate - an XML document, you need an XML parser. 
bulletThe 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)
bulletOnce you have installed IE, the parser is available to scripts inside HTML documents and ASP files.
bulletThe Microsoft XMLDOM parser features a language-neutral programming model that:
bulletSupports JavaScript, VBScript, Perl, VB, Java, C++ and more
bulletSupports W3C XML 1.0 and XML DOM
bulletSupports DTD and validation
bulletIf you are using JavaScript in IE, you can create an XML document object with the following code:
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
bulletIf you are using VBScript you create the XML document object with the following code:
set xmlDoc = CreateObject("Microsoft.XMLDOM")
bulletIf 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

bulletThe 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>
bulletThe first line of the script creates an instance of the Microsoft XML parser. 
bulletThe third line tells the parser to load an XML document called note.xml. 
bulletThe second line assures that the parser will halt execution until the document is fully loaded.

Loading pure XML text into the parser

bulletThe 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>
bulletNote that the "loadXML" method (instead of the "load" method) is used to load a text string.

 

Parser Errors

The parseError Object

bulletIf you try to open an XML document, the XML Parser might generate an error. 
bulletBy accessing the parseError object, the exact error code, the error text, and even the line that caused the error can be retrieved:

File Error

bulletIn 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

bulletNow, 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

bulletOne 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. 
bulletA small snippet of programming code like a VBScript for/each construct can be written to demonstrate this.
bulletThe 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

bulletBy 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. 
bulletWhen you add that these JavaScripts can access Active Server Pages from a Web server, the future looks very bright.
bulletThe 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

bulletThe 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)
 

Home ] Up ] Computer Architecture ] Programming Bootcamp ] Database Bootcamp ] Visual BasicS ] Web Basics ] Web Multimedia ] Web Programming ] Advanced Web Topics ] Developing Web Sites ] Web Glossary ]

Copyright © 1999 - 2005 
ThePlace - Written and Sponsored by Dave Hillman