DOM Overview - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

DOM Overview

Description:

Comment. Document. Object. Element. Text. CDATA. Text. Element. Comment ... be an Element, Processing Instruction, Comment, Text, CDATA Section, or Entity Reference. ... – PowerPoint PPT presentation

Number of Views:56
Avg rating:3.0/5.0
Slides: 28
Provided by: jonmil
Category:
Tags: dom | comment | overview

less

Transcript and Presenter's Notes

Title: DOM Overview


1
Unit 2
  • DOM Overview

2
Section Objectives
  • W3C and DOM
  • XML as a Tree
  • DOM Object
  • Exploring DOM Interface
  • SAX

3
Document Object Model
  • W3C Recommendation
  • Use with ECMAScript
  • Well Established

4
DOM Features and W3C Recommendations
  • Access to Structure of well-formed XML
  • Navigate the XML Document Object
  • XML Document is a file
  • DOM is an Object
  • W3C Recommendation
  • Level 1
  • Level 2

5
Section Objectives
  • W3C and DOM
  • XML as a Tree
  • DOM Object
  • Exploring DOM Interface
  • SAX

6
XML Document as Tree
P1
S2
S1
B1
Example1.xml
7
Terminology of Tree Elements
  • Root
  • Children
  • Descendents
  • Sibling
  • Sub-tree
  • Leaf
  • Parent

8
Section Objectives
  • W3C and DOM
  • XML as a Tree
  • DOM Object
  • Exploring DOM Interface
  • SAX

9
Basic DOM Objects
  • Base form
  • Specific Node objects
  • Elements
  • Attributes
  • etc.
  • Collections

10
Document Object and its Creation
import org.apache.xerces.parsers.DOMParser //
other import declarations //create a DOM parser
called 'parser' DOMParser parser new
DOMParser() //retrieve and parse XML document
from address URL or file parser.parse(URL) //
Create a DOM object (doc) - this represent the
top-level reference // to the whole DOM
structure. Elements can be created from this
object. Document doc parser.getDocument() //
Return the root element (node) of the document
object Element root doc.getDocumentElement()
11
Base Objects
  • Node
  • NodeList
  • NamedNodeMap

12
XML Node Hierarchy
Document Type
Comment
Document Object
Processing Instruction
Text
Attribute
Entity Reference
Element
Comment
An entity reference can be an Element, Processing
Instruction, Comment, Text, CDATA Section, or
Entity Reference.
Element
Text
CDATA
13
Section Objectives
  • W3C and DOM
  • XML as a Tree
  • DOM Object
  • Exploring DOM Interface
  • SAX

14
Exploring the DOM Interface
// Create a DOM object (doc) - this represent the
top-level reference // to the whole DOM
structure. Elements can be created from this
object Document XMLDoc parser.getDocument() //
return the root element (node) of the document
object Element root doc.getDocumentElement()
// Get node name, value, and type String name
root.getNodeName() String value
root.getNodeValue() Short type
root.getNodeType() // Get attributes NamedNodeMa
p attributes root.getAttributes() // Get
child nodes NodeList children
root.getChildNodes()
15
Viewing DOM Model as XML
lt?xml version 1.0?gt ltparagraph ID
P1gt ltsentence ID S1gt We have already looked
at the tree structure as a model of an XML
document in the introduction to the
course lt/sentencegt ltsentence ID
S2gt Understanding this structure is ltbold ID
B1gtessentiallt/boldgt to understanding the DOM
representation of the document so we shall
briefly look at it again based on a simple
document whose meaning should be obvious and its
content familiar. lt/sentencegt lt/paragraphgt
Example2.xml
16
Iterating Nodes of DOM
17
Basic Node Properties
  • nodeName
  • nodeType
  • nodeValue
  • childNodes
  • childNodes.length
  • attributes
  • attributes.length

18
Basic Node Properties Java Example
  • public Example3 (String fileName)
  • try
  • source new InputSource(new FileReader(fileName
    ))
  • catch (IOException e) ...
  • String analysis ""
  • DOMParser parser new DOMParser()
  • parser.parse(source)
  • // Create a new DOM
  • Document doc parser.getDocument()
  • // return root element of the document
  • Element root doc.getDocumentElement()
  • // Display level, name, type, value for current
    node
  • analysis buildProperties(doc,0)
  • return analysis

Example3.java
19
Basic Node Properties Java Example
  • StringBuffer buffer new StringBuffer()
  • buffer.append(spaces(depth c) "Level "
    depth "\n")
  • buffer.append(spaces(depth c)
  • "Node name " node.getNodeName() "\n")
  • buffer.append(spaces(depth c)
  • "Node type " node.getNodeType() "\n")
  • buffer.append(spaces(depth c)
  • "Node value " node.getNodeValue() "\n")
  • buffer.append(spaces(depth d 10)
  • "Attribute name " attributes.item(i).getNodeN
    ame()
  • "\n")
  • buffer.append(spaces(depth d 10)
  • "Attribute type " attributes.item(i).getNodeT
    ype()
  • "\n")
  • buffer.append(spaces(depth d 10)
  • "Attribute value " attributes.item(i).getNodeV
    alue()
  • "\n")

Example3.java
20
Collection Objects
21
Searching for Specific Values
  • lt?xml version "1.0"?gt
  • ltcourseNotesgt
  • ltSection num "1"gt
  • ltheadinggtOrigins of XMLlt/headinggt
  • ltcontentgtThis unit will explain the origins of
    XMLlt/contentgt
  • ...
  • ltheadinggtParsing XML Documents in IE5lt/headinggt
  • ltcontentgtThis...lt/contentgt
  • lt/Sectiongt
  • lt/courseNotesgt
  • JOptionPane.showMessageDialog(
  • null, appl.searchNodes(), "Properties",
  • JOptionPane.INFORMATION_MESSAGE)
  • Node nextSecChild
  • root.getChildNodes().item(i)
  • if (nextSecChild.getNodeType() ! Node.TEXT_NODE)
  • Node nextTitleChild nextSecChild.getChildNodes(
    ).item(j)
  • String sItem
  • nextTitleChild.getNodeName()
  • if (sItem.equals("heading"))

Example4.java
Example4.xml
22
Filtering a NodeList by TagName
NodeList nodeList doc.getElementsByTagName("Sect
ion") output displayHeadings(nodeList) NodeL
ist nextSecChild nodeList.item(i).getChildNod
es() buffer.append(nodeList.item(i).getNodeName()
"" (i 1) "\n") int numTitleChildren
nextSecChild.getLength() int j // loop for
each child node of section node for (j 0 j lt
numTitleChildren j) String sItem
nextSecChild.item(j).getNodeName() // if node
name is "heading" output text node value if
(sItem.equals("heading"))
Example5.java using Example4.xml
23
Selecting Nodes from Attributes
  • public String findByAttribute(
  • String elementName,
  • String attributeName,
  • String attributeValue)
  • NodeList sections doc.getElementsByTagName(eleme
    ntName)
  • NamedNodeMap attributeList sections.item(i).getA
    ttributes()
  • if (attributeList.item(j).getNodeName().equals(att
    ributeName))
  • String nodeValue attributeList.item(j).getNodeV
    alue()
  • // if attribute value found then "BINGO"
  • if (nodeValue.equals(attributeValue))
  • s "BINGO!!!"

Example6.java using Example4.xml
24
Section Objectives
  • W3C and DOM
  • XML as a Tree
  • DOM Object
  • Exploring DOM Interface
  • SAX

25
SAX an Alternative to DOM
  • Event Driven
  • Better for Large XML Files
  • Most parsers support DOM and SAX
  • Example Xerces
  • More information
  • http//www.megginson.com/SAX/index.html

26
SAX Benefits
  • Handle Files of Any Size
  • When You Want to Build a Data Structure
  • Want Small Subset of Document
  • Simple
  • Fast

27
SAX Drawbacks
  • No Random Access to Document
  • Complex Searches Difficult
  • DTD Not Available
  • Lexical Information Not Available
  • Read-Only
Write a Comment
User Comments (0)
About PowerShow.com