Title: DOM Overview
1Unit 2
2Section Objectives
- W3C and DOM
- XML as a Tree
- DOM Object
- Exploring DOM Interface
- SAX
3Document Object Model
- W3C Recommendation
- Use with ECMAScript
- Well Established
4DOM 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
5Section Objectives
- W3C and DOM
- XML as a Tree
- DOM Object
- Exploring DOM Interface
- SAX
6XML Document as Tree
P1
S2
S1
B1
Example1.xml
7Terminology of Tree Elements
- Root
- Children
- Descendents
- Sibling
- Sub-tree
- Leaf
- Parent
8Section Objectives
- W3C and DOM
- XML as a Tree
- DOM Object
- Exploring DOM Interface
- SAX
9Basic DOM Objects
- Base form
- Specific Node objects
- Elements
- Attributes
- etc.
- Collections
10Document 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()
11Base Objects
- Node
- NodeList
- NamedNodeMap
12XML 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
13Section Objectives
- W3C and DOM
- XML as a Tree
- DOM Object
- Exploring DOM Interface
- SAX
14Exploring 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()
15Viewing 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
16Iterating Nodes of DOM
17Basic Node Properties
- nodeName
- nodeType
- nodeValue
- childNodes
- childNodes.length
- attributes
- attributes.length
18Basic 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
19Basic 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
20Collection Objects
21Searching 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
22Filtering 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
23Selecting 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
24Section Objectives
- W3C and DOM
- XML as a Tree
- DOM Object
- Exploring DOM Interface
- SAX
25SAX 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
26SAX Benefits
- Handle Files of Any Size
- When You Want to Build a Data Structure
- Want Small Subset of Document
- Simple
- Fast
27SAX Drawbacks
- No Random Access to Document
- Complex Searches Difficult
- DTD Not Available
- Lexical Information Not Available
- Read-Only