introduction to the document object model eugenia fernandez iupui
TRANSCRIPT
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 NodesNode 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
DocumentElement
Element
Attr
Text
Element
Text
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
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.
<xml>
<order> <orderitem title=“XML by Example” isbn=“12345675”/> </order>
</xml>
Create the Document Object: Version 1
From XML data island
The XMLDocument property of the data island provides a reference to the Document object
<XML id=“booksdso” src=“books.xml”></XML>
Set doc = booksdso.XMLDocument
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
alternatively, you can load an XML string instead of a file
Set doc = CreateObject(“Microsoft.XMLDOM”)
[or set doc = CreateObject(“MSXML2.DOMDocument”)]
doc.async = False
doc.load “Books.xml”
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
parent node, if any childNode
set of child nodes firstChild, lastChild previousSibling, nextSibling attributes
set of attributes of the current node, if any
Navigating Elements
DOCUMENT
theNode
theNode.parentNode.lastChild
theNode.parentNode.childNodes(0)
theNode.parentNode
theNode.ownerDocument
theNode.nextSibling
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 nodesset doc = booksdso.XMLDocumentset rootnode = doc.documentElementfor each child in rootNode.childNodes ‘process data in child nodenext
‘orset children = rootNode.childNodesfor 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
This retrieves the value of the first attribute of the first child node of the root element (doc.documentElement)
doc.documentElement.firstChild.attributes(0).value
Microsoft Extensions nodeTypeString
type of node expressed as a string, e.g. “document”, “element”, “attribute”
text 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.xmlMsgbox booksdso.XMLDocument.document element.xml