domIntro

Introduction to the
Document Object Model
Eugenia Fernandez
IUPUI
Document Object Model (DOM)

XML DOM is an object model that
exposes the contents of an XML
document.


Exposed means that the content of the
document can be accessed and
manipulated – via programs or scripts.
The DOM defines a set of objects
that allow the nodes of an XML
document tree to be accessed and
modified.
DOM Nodes
Node
Description
Node
Any document component such as an element,
attribute, comments or text string
Document
The entire document, comprising all nodes
Element
A document element
Attr
An attribute of an element as a name/value
pair
Processing
Any processing instruction encoded in the
document
Instruction
Comments, ignored by the parser
Text
Text content of an element
The DOM Tree Structure


Recall that an XML
parser takes an
XML document and
creates a tree
structure to
represent the
document.
The tree structure
shows the
hierarchy of data
in the XML
document.
DOCUMENT
Document
Element
Element
Attr
Text
Text
Element
Text
Everything is a Node


Every ‘branch’ of the XML DOM tree is a
Node object.
Common Node methods


firstChild, lastChild, nextSibling,
previousSibling
Common Node properties



nodeValue
nodeType
nodeName
Node
nodeType value
Document
NODE_DOCUMENT
Element
NODE_ELEMENT
Attr
NODE_ATTRIBUTE
Text
NODE_TEXT
Example
<xml>
<order>
<orderitem title=“XML by Example” isbn=“12345675”/>
</order>
</xml>
The entire XML document is a Document node,
<order> and <orderitem> are Element nodes,
title and isbn are Attr nodes, and the values
“XML by Example” and “12345675” are Text
nodes.
Create the Document Object:
Version 1

From XML data island
<XML id=“booksdso” src=“books.xml”></XML>
Set doc = booksdso.XMLDocument

The XMLDocument property of the data island provides
a reference to the Document object
Create the Document Object:
Version 2

From external XML file



create a DOM document using the CreateObject method
set the async property to False to indicate NOT to load the
XML file asynchronously, if loaded async, the parser halts
execution until the entire document is loaded
load the XML file into the Document object
Set doc = CreateObject(“Microsoft.XMLDOM”)
[or set doc = CreateObject(“MSXML2.DOMDocument”)]
doc.async = False
doc.load “Books.xml”
alternatively, you can load an XML string instead of a file
doc.load “<?xml version=‘1.0’?><booklist></booklist>”
Accessing Nodes

Within a DOM object, once you access a
particular node you can use its
properties/methods to determine its






location in the document tree
child nodes
parent node
siblings and ancestors
attributes
Accessing the root element

set rootNode = doc.documentElement
Node Properties & Methods


nodeType, nodeName, nodeValue
parentNode


childNode




parent node, if any
set of child nodes
firstChild, lastChild
previousSibling, nextSibling
attributes

set of attributes of the current node, if any
Navigating Elements
DOCUMENT
theNode.ownerDocument
theNode.parentNode
theNode.parentNode.childNodes(0)
theNode
theNode.nextSibling
theNode.parentNode.lastChild
Collections
Each node potentially has a
collection, or set, of child nodes.
 Collections have methods used to
traverse the nodes sequentially.
 Numbering starts at 0.

Navigating Node Collections

childNodes property returns a NodeList
object – a zero-based collection of nodes
set doc = booksdso.XMLDocument
set rootnode = doc.documentElement
for each child in rootNode.childNodes
‘process data in child node
next
‘or
set children = rootNode.childNodes
for I=0 to children.length-1
‘process data in children.item(I)
Next
Retrieving Node Content

nodeType

type of node expressed by number
• 9 = document node
• 1 = element node
• 2 = attribute node

nodeName

name of the node
Retrieving a Node’s Value

nodeValue


value of a text node or attribute node
The nodeValue of an element is null.
Its text is held in a child text node.
To retrieve its value, you must
retrieve the value of first child.
<book title=“XML by Example”>
set title = rootNode.firstChild.childNodes(0)
set titleValue = title.firstChild.nodeValue
The Document Object



The topmost node in a DOM tree (NOT
the root XML element, but above that –
represents the entire document)
This is set by the creation of the DOM
tree (either version 1 or version 2).
Has two properties:

documentElement
• this is the root element of the XML document
• accessed as doc.documentElement

doctype
• not specified in DOM level 1
The Element Object
Represents XML elements
 Adds new property:



tagName
Adds new method

getElementByTagName()
• returns a node set of all descendants of the
element with a given tag name
Accessing Attributes
Use the attributes property to
access the value of attributes
 Example

doc.documentElement.firstChild.attributes(0).value
This retrieves the value of the first attribute
of the first child node of the root element
(doc.documentElement)
Microsoft Extensions

nodeTypeString


text


type of node expressed as a string, e.g.
“document”, “element”, “attribute”
returns text content of the node and all
its descendants
xml

returns XML of the node and all its
descendants
Viewing XML Data

Microsoft’s DOM defines an xml
property in the Node object. This
allows you to retrieve the XML data
contained in that portion of the tree,
i.e. the subtree that starts at the
selected node.
Msgbox booksdso.XMLDocument.xml
Msgbox booksdso.XMLDocument.document element.xml
Source

“Building XML-Based Web
Applications” a Microsoft Certified
Course.