DOM

Definition

DOM

= Document Object Model

Tree-based parser

converts document into XML DOM-object that can be accessed via the API.

W3C standard for reading, changing, adding, deleting XML elements.

Tree based Parser

Map XML document to internal tree structure in main memory.

App can read structure from main memory.

Used for small documents with ready-made data structure

  • Random access
  • Slow
  • Occupied memory constant to document size

Node Interface

Loading an XMLDocument

import javax.xml.parsers.*;
import org.w3c.dom. *;
public class Course {public static void main(String[] args) throws Exception {
//factory instantiation: API that enables applications to obtain a parser that
//produces DOM object trees from XML documents
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//validation and namespaces
factory.setValidating(true);
factory.setNamespaceAware(true);
//parser instantiation
//API to obtain DOM document instances from XML documents
DocumentBuilder builder = factory.newDocumentBuilder();
//install ErrorHandler
builder.setErrorHandler(new MyErrorHandler());
//parsing instantiation (code below is written from this line onwards)
Document coursedoc = builder.parse(args[0]);
}
}

The Node interface is the datatype of the entire DOM.

More specific subinterfaces inherit from it: Document , Element , Attr , Text , ...

Example

  • Visit all child nodes of “courses.xml”
    visitNode(coursedoc.getDocumentElement());private void visitNode(Node node) {
    //iterate over all children
    for (int i = 0; i < node.getChildNodes().getLength(); i++) {
    //recursively visit all nodes
    visitNode(node.getChildNodes().item(i));
    }
    }

Node Interface

Document Methods (child interface of Node)

Element Methods (child interface of Node)

Attr Methods (child interface of Node)

Example

<?xml version="1.0"?>
<courses>
<course semester="Summer">
<title> Semi-structured Data (SSD) </title>
<day> Thursday </day>
<time> 09:15 </time>
<location> HS8 </location>
</course>
</courses>
courses:
course: semester="Summer"
title: "Semi-structured Data (SSD)"
day: "Thursday"
time: "09:15"
location: "HS8"