Parsing with DOM using MSXML Kanda Runapongsa krunaponkku

  • Slides: 18
Download presentation
Parsing with DOM using MSXML Kanda Runapongsa (krunapon@kku. ac. th) Dept. of Computer Engineering

Parsing with DOM using MSXML Kanda Runapongsa (krunapon@kku. ac. th) Dept. of Computer Engineering Khon Kaen University 168493: XML and Web Services (II/2546)

XML DOM The XML Document Object Model (DOM) is a Programming Interface for XML

XML DOM The XML Document Object Model (DOM) is a Programming Interface for XML documents n It defines the way an XML document can be accessed and manipulated n It is designed to be used with several programming languages and any operating system n 168493: XML and Web Services (II/2546) 2

Introduction to DOM A program called an XML parser can be used to load

Introduction to DOM A program called an XML parser can be used to load an XML document into the memory n When the document is loaded, its information can be retrieved and manipulated by accessing the DOM n 168493: XML and Web Services (II/2546) 3

Overview of DOM The DOM represents a tree view of the XML document n

Overview of DOM The DOM represents a tree view of the XML document n The document. Element is the top-level of the tree n This element has one or many child. Nodes that represent the branches of the tree n 168493: XML and Web Services (II/2546) 4

The Microsoft XML Parser Supports Java. Script, VBScript, Perl, VB, Java, C++ and more

The Microsoft XML Parser Supports Java. Script, VBScript, Perl, VB, Java, C++ and more n Supports W 3 C XML 1. 0, XML DOM, and SAX n Supports DTD and XSD n The Microsoft XML parser is a COM component that comes with Microsoft Internet Explorer n 168493: XML and Web Services (II/2546) 5

Creating an XML Document Object n Javascript n n var xml. Doc = new

Creating an XML Document Object n Javascript n n var xml. Doc = new Active. XObject("Microsoft. XMLDOM") VBScript n set xml. Doc = Create. Object("Microsoft. XMLDOM") 168493: XML and Web Services (II/2546) 6

Loading an XML File <script type="text/javascript"> var xml. Doc = new Active. XObject("Microsoft. XMLDOM")

Loading an XML File <script type="text/javascript"> var xml. Doc = new Active. XObject("Microsoft. XMLDOM") xml. Doc. async="false" xml. Doc. 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. n A

DOM Node The node object represents a node in the node tree. n A node can be an element node, a text node, or any other of the node types n All of these node types have properties and methods n 168493: XML and Web Services (II/2546) 8

DOM Node Properties Property Description attributes Returns all attributes child. Nodes Returns a Node.

DOM Node Properties Property Description attributes Returns all attributes child. Nodes Returns a Node. List containing all the child nodes Returns the parent node parent. Node first. Child Returns the first child node for this node 168493: XML and Web Services (II/2546) 9

DOM Node Properties Property Description first. Child Returns the first child node last. Child

DOM Node Properties Property Description first. Child Returns the first child node last. Child Returns the last child node next. Sibling Returns the next sibling node previous. Sibling Returns the previous sibling node 168493: XML and Web Services (II/2546) 10

DOM Node Properties Property Description node. Type Returns the node. Type as a number

DOM Node Properties Property Description node. Type Returns the node. Type as a number node. Value Returns the value of this node owner. Document Returns the root node of the document node. Name Returns the node. Name 168493: XML and Web Services (II/2546) 11

DOM Node Methods Method Description append. Child (new. Child) Appends the node new. Child

DOM Node Methods Method Description append. Child (new. Child) Appends the node new. Child at the end of the child nodees has. Child. Nodes Returns true if this node has () any child nodes 168493: XML and Web Services (II/2546) 12

DOM Node Methods Method Description insert. Before (new. Node, ref. Node) remove. Child (node.

DOM Node Methods Method Description insert. Before (new. Node, ref. Node) remove. Child (node. Name) replace. Child (new. Node, old. Node) Inserts a new node, new. Node, before the existing node ref. Node Removes the specified node, node. Name Replaces the old. Node, with the new. Node 168493: XML and Web Services (II/2546) 13

Node Types Node. Type Named Constants 1 ELEMENT_NODE 2 ATTRIBUTE_NODE 3 TEXT_NODE 4 CDATA_SECTION_NODE

Node Types Node. Type 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 Node. Type Named Constants 7 8 PROCESSING_INSTRUCTION_N ODE COMMENT_NODE 9 DOCUMENT_NODE 10

Node Types Node. Type 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 Node. List Object n n n length: return the number of nodes in

DOM Node. List Object n n n length: return the number of nodes in a node. List item: return a specific node in the node. List Examples: xml. Doc. document. Element. child. Nodes. leng th xml. Doc. document. Element. child. Nodes. item (2) 168493: XML and Web Services (II/2546) 16

DOM Elements tag. Name: return the tag name of a node n get. Elements.

DOM Elements tag. Name: return the tag name of a node n get. Elements. By. Tag. Name: return the value of a specified node n get. Attribute: return an attribute’s value n 168493: XML and Web Services (II/2546) 17

DOM Attributes name: return the name of an attribute n Value: return the value

DOM Attributes name: return the name of an attribute n Value: return the value of an attribute n Example: for each x in xml. Doc. document. Element. attributes document. write(x. name) document. write(x. value) next n 168493: XML and Web Services (II/2546) 18