Docsity

SAX and DOM: A Comparison of XML Parsing Technologies, Slides of Computer Science

Partial preview of the text

Download SAX and DOM: A Comparison of XML Parsing Technologies and more Slides Computer Science in PDF only on Docsity!

DOM

SAX and DOM

• SAX and DOM are standards for XML parsers--

program APIs to read and interpret XML files

  • DOM is a W3C standard
  • SAX is an ad-hoc (but very popular) standard

• There are various implementations available

• Java implementations are provided in JAXP (Java

API for XML Processing)

• JAXP is included as a package in Java 1.

  • JAXP is available separately for Java 1.

• Unlike many XML technologies, SAX and DOM are

relatively easy

Simple DOM program, I

  • This program is adapted from CodeNotes® for XML by Gregory

Brill, page 128

  • import javax.xml.parsers.*;

import org.w3c.dom.*;

  • public class SecondDom {

public static void main(String args[]) {

try {

...Main part of program goes here...

} catch (Exception e) {

e.printStackTrace(System.out);

Simple DOM program, II

• First we need to create a DOM parser, called a

“DocumentBuilder”

• The parser is created, not by a constructor, but by

calling a static factory method

  • This is a common technique in advanced Java programming
  • The use of a factory method makes it easier if you later switch to a different parser

DocumentBuilderFactory factory =

DocumentBuilderFactory.newInstance();

DocumentBuilder builder =

factory.newDocumentBuilder();

Simple DOM program, IV

• The following code finds the content of the

root element and prints it:

Element root = document.getDocumentElement();

Node textNode = root.getFirstChild();

System.out.println(textNode.getNodeValue());

• This code should be mostly self-explanatory;

we’ll get into the details shortly

• The output of the program is: Hello World!Docsity.com

Reading in the tree

• The parse method reads in the entire XML

document and represents it as a tree in

memory

– For a large document, parsing could take a while

– If you want to interact with your program while

it is parsing, you need to parse in a separate

thread

  • Once parsing starts, you cannot interrupt or stop it
  • Do not try to access the parse tree until parsing is done

• An XML parse tree may require up to ten

times as much memory as the original XML

document

Operations on Nodes, I

  • The results returned by getNodeName(), getNodeValue(),

getNodeType() and getAttributes() depend on the

subtype of the node, as follows:

Element Text Attr

getNodeName()

getNodeValue()

getNodeType()

getAttributes()

tag name

null

ELEMENT_NODE

NamedNodeMap

"#text"

text contents

TEXT_NODE

null

name of attribute

value of attribute

ATTRIBUTE_NODE

null

Distinguishing Node types

  • Here’s an easy way to tell what kind of a node you are dealing

with:

switch(node.getNodeType()) { case Node.ELEMENT_NODE: Element element = (Element)node; ...; break; case Node.TEXT_NODE: Text text = (Text)node; ... break; case Node.ATTRIBUTE_NODE: Attr attr = (Attr)node; ... break; default: ... } Docsity.com

Operations for Elements

  • String getTagName()
    • Returns the name of the tag
  • boolean hasAttribute(String name)
    • Returns true if this Element has the named attribute
  • String getAttribute(String name)
    • Returns the (String) value of the named attribute
  • boolean hasAttributes()
    • Returns true if this Element has any attributes
    • This method is actually inherited from Node
      • Returns false if it is applied to a Node that isn’t an Element
  • NamedNodeMap getAttributes()
    • Returns a NamedNodeMap of all the Element’s attributes
    • This method is actually inherited from Node
      • Returns null if it is applied to a Node that isn’t an Element

NamedNodeMap

  • The node .getAttributes() operation returns a

NamedNodeMap

  • Because NamedNodeMaps are used for other kinds of nodes (elsewhere in Java), the contents are treated as general Nodes, not specifically as Attrs
  • Some operations on a NamedNodeMap are:
  • getNamedItem(String name) returns (as a Node) the attribute with the given name
  • getLength() returns (as an int) the number of Nodes in this NamedNodeMap
  • item(int index) returns (as a Node) the index th^ item
  • This operation lets you conveniently step through all the nodes in the NamedNodeMap
  • Java does not guarantee the order in which nodes are returned

Operations on Attrs

• String getName()

– Returns the name of this attribute.

• Element getOwnerElement()

– Returns the Element node this attribute is

attached to, or null if this attribute is not in use

• boolean getSpecified()

– Returns true if this attribute was explicitly given a

value in the original document

• String getValue()

– Returns the value of the attribute as a String Docsity.com

Preorder traversal

• The DOM is stored in memory as a tree

• An easy way to traverse a tree is in preorder

• You should remember how to do this from

your course in Data Structures

• The general form of a preorder traversal is:

– Visit the root

– Traverse each subtree, in order

Trying out the program

Input:

The Beginning The Middle The End

Output:

1 novel null 3 #text null 1 chapter null num="1“ 3 #text The Beginning null 3 #text null 1 chapter null num="2“ 3 #text The Middle null 3 #text null 1 chapter null num="3“ 3 #text The End null 3 #text null

Things to think about:

What are the numbers?

Are the nulls in the right places?

Is the indentation as expected?

How could this program be improved?

Additional DOM operations

• I’ve left out all the operations that allow you

to modify the DOM tree, for example:

– setNodeValue(String nodeValue )

– insertBefore(Node newChild , Node refChild )

• Java provides a large number of these

operations

• These operations are not part of the W3C

specifications

• There is no standardized way to write out a

DOM as an XML document

– It isn’t that hard to write out the XML Docsity.com