Parsing with DOM using MSXML Kanda Runapongsa ([email protected]) Dept. of Computer Engineering Khon Kaen University XML DOM The XML Document Object Model (DOM) is a Programming Interface for XML documents It defines the way an XML document can be accessed and manipulated It is designed to be used with several programming languages and any operating system 168493: XML and Web Services (II/2546) 2 Introduction to DOM A program called an XML parser can be used to load an XML document into the memory When the document is loaded, its information can be retrieved and manipulated by accessing the DOM 168493: XML and Web Services (II/2546) 3 Overview of 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 168493: XML and Web Services (II/2546) 4 The Microsoft XML Parser Supports JavaScript, VBScript, Perl, VB, Java, C++ and more Supports W3C XML 1.0, XML DOM, and SAX Supports DTD and XSD The Microsoft XML parser is a COM component that comes with Microsoft Internet Explorer 168493: XML and Web Services (II/2546) 5 Creating an XML Document Object Javascript var xmlDoc = new ActiveXObject("Microsoft.XMLDOM") VBScript set xmlDoc = CreateObject("Microsoft.XMLDOM") 168493: XML and Web Services (II/2546) 6 Loading an XML File <script type="text/javascript"> var xmlDoc = new ActiveXObject("Microsoft.XMLDOM") xmlDoc.async="false" xmlDoc.load(“cdcatalog.xml") // ....... processing the document goes here </script> 168493: XML and Web Services (II/2546) 7 DOM Node The node object represents a node in the node tree. A node can be an element node, a text node, or any other of the node types All of these node types have properties and methods 168493: XML and Web Services (II/2546) 8 DOM Node Properties Property Description attributes Returns all attributes childNodes Returns a NodeList containing all the child nodes Returns the parent node parentNode firstChild Returns the first child node for this node 168493: XML and Web Services (II/2546) 9 DOM Node Properties Property Description firstChild Returns the first child node lastChild Returns the last child node nextSibling Returns the next sibling node previousSibling Returns the previous sibling node 168493: XML and Web Services (II/2546) 10 DOM Node Properties Property Description nodeType Returns the nodeType as a number nodeValue Returns the value of this node ownerDocument Returns the root node of the document nodeName Returns the nodeName 168493: XML and Web Services (II/2546) 11 DOM Node Methods Method Description appendChild (newChild) Appends the node newChild at the end of the child nodees hasChildNodes Returns true if this node has () any child nodes 168493: XML and Web Services (II/2546) 12 DOM Node Methods Method Description insertBefore (newNode, refNode) removeChild (nodeName) replaceChild (newNode, oldNode) Inserts a new node, newNode, before the existing node refNode Removes the specified node, nodeName Replaces the oldNode, with the newNode 168493: XML and Web Services (II/2546) 13 Node Types NodeType Named Constants 1 ELEMENT_NODE 2 ATTRIBUTE_NODE 3 TEXT_NODE 4 CDATA_SECTION_NODE 5 ENTITY_REFERENCE_NODE 6 ENITY_NODE 168493: XML and Web Services (II/2546) 14 Node Types NodeType Named Constants 7 8 PROCESSING_INSTRUCTION_N ODE COMMENT_NODE 9 DOCUMENT_NODE 10 DOCUMENT_TYPE_NODE 11 DOCUMENT_FRAGMENT_NODE 12 NOTATION_NODE 168493: XML and Web Services (II/2546) 15 DOM NodeList Object length: return the number of nodes in a nodeList item: return a specific node in the nodeList Examples: xmlDoc.documentElement.childNodes.leng th xmlDoc.documentElement.childNodes.item (2) 168493: XML and Web Services (II/2546) 16 DOM Elements tagName: return the tag name of a node getElementsByTagName: return the value of a specified node getAttribute: return an attribute’s value 168493: XML and Web Services (II/2546) 17 DOM Attributes name: return the name of an attribute Value: return the value of an attribute Example: for each x in xmlDoc.documentElement.attributes document.write(x.name) document.write(x.value) next 168493: XML and Web Services (II/2546) 18
© Copyright 2026 Paperzz