Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to the Document Object Model

Similar presentations


Presentation on theme: "Introduction to the Document Object Model"— Presentation transcript:

1 Introduction to the Document Object Model
Eugenia Fernandez IUPUI

2 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.

3 DOM Nodes Node Description
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

4 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

5 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

6 Example <xml> <order> <orderitem title=“XML by Example” isbn=“ ”/> </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 “ ” are Text nodes.

7 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

8 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>”

9 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

10 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

11 Navigating Elements DOCUMENT theNode theNode.ownerDocument
theNode.parentNode theNode.parentNode.childNodes(0) theNode theNode.nextSibling theNode.parentNode.lastChild

12 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.

13 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

14 Retrieving Node Content
nodeType type of node expressed by number 9 = document node 1 = element node 2 = attribute node nodeName name of the node

15 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

16 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

17 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

18 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

19 Microsoft Extensions nodeTypeString text xml
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

20 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

21 Source “Building XML-Based Web Applications” a Microsoft Certified Course.


Download ppt "Introduction to the Document Object Model"

Similar presentations


Ads by Google