51
SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

Embed Size (px)

Citation preview

Page 1: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

SE 5145 – eXtensible Markup Language (XML )

DOM (Document Object Model)

(Part II – Java API)

2011-12/Spring, Bahçeşehir University, Istanbul

Page 2: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

4th Assignment: DOM implementation of XML Resume

Implement DOM to your Resume.

Required: There must exist a Checkbox containing the labels "Technical" and "Management". Upon checking the label(s), relevant sections of your Resume must be highligted with different colors.

Optional: A "Show experience duration" button should calculate the sum of check-ed experiences.

You can write the implementation by using one of the following ways;

a) Javascript (Due April 27) or

b) Java, C#, or any other language you prefer (Due May11)

Notes:

1. A sample Javascript based DOM implementation is displayed on the next slide. Relevant files will also be sent to you.

2. Details of the assignment was already discussed at the last lecture. If you did not attend it, please ask the details to your classmates, not to me since nowadays I' m not available to reply individual emails.

2

Page 3: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

JAXP (Java API for XML Processing)

Standard Java API for loading, creating, accessing, and transforming XML documents

JAXP provides a vendor-neutral interface to the underlying DOM or SAX parser Parsers distribute implementation configured to return appropriate parser instances

Page 4: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

javax.xml.parsers

DOM DocumentBuilder, DocumentBuilderFactory

SAX SAXParser, SAXParserFactory

General FactoryConfigurationError ParserConfigurationException

Page 5: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

DOM: Parsing Document

Get instance of document builder factory DocumentBuilderFactory factory =

DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); factory.setValidating(false);

Create document builder DocumentBuilder builder = factory.newDocumentBuilder();

Parse document Document document = builder.parse(“document.xml”);

Page 6: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

DOM: Creating Empty Document

Get instance of document builder factory DocumentBuilderFactory factory =

DocumentBuilderFactory.newInstance();

Create document builder DocumentBuilder builder = factory.newDocumentBuilder();

Create document Document document = builder.newDocument();

Page 7: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

SAX: Parsing Document

Get instance of SAX parser factory SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); factory.setValidating(false);

Create SAX parser SAXParser parser = factory.newSAXParser();

Parse document parser.parse(“document.xml”, new MyHandler());

Page 8: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

Configuration

DocumentBuilderFactory Allows setting of “attributes”

String identifier Object value

SAXParserFactory Allows setting of “features”

String identifier boolean state

SAXParser allows setting of “properties” String identifier, Object value

Page 9: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

Error Handling and Entity Resolution

DocumentBuilder Can set a SAX ErrorHandler Can set a SAX EntityResolver

SAXParser Must query XMLReader first, then

Can set ErrorHandler Can set EntityResolver

Page 10: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

DOM Installation and Setup

All the necessary classes for DOM and JAXP are included with JDK 1.4 See javax.xml.* packages

For DOM and JAXP with JDK 1.3 see following viewgraphs

10

Page 11: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

DOM Installation and Setup (JDK 1.3)

11

Page 12: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

DOM Installation and Setup (JDK 1.3)

12

Page 13: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

DOM Installation and Setup (JDK 1.3)

13

Page 14: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

Steps for DOM Parsing

14

Page 15: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

Step 1: Specifying a Parser

15

Page 16: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

Step 1: Example

16

Page 17: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

Step 2: Create a JAXP Document Builder

17

Page 18: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

Step3: Invoke the Parser to Create a Document

18

Page 19: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

Step 4: Normalize the Tree

19

Page 20: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

Step 5: Obtain the Root Node of the Tree

20

Page 21: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

Step 6: Examine and Modify Properties of the Node

21

Page 22: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

Step 6: Examine and Modify Properties of the Node (contd)

22

Page 23: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

Step 6: Examine and Modify Properties of the Node (contd)

23

Page 24: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

DOM Example1: Accessing XML file from Java

24

This example shows how to access the XML file through Java. For that we have used DOM parser. DOM parser loads the XML file into the memory and makes an object model which can be traversed to get the file elements. For this, two different files are used:

1) MyFile.xml2) AccessingXmlFile.java 

Page 25: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

DOM Example1: Accessing XML file from Java

25

 

Page 26: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

DOM Example1: Accessing XML file from Java

26

 

2) AccessingXmlFile.java

The method DocumentBuilderFactory.newInstance() enables applications to obtain a parser that produces DOM. The DocumentBuilder provides the DOM document instances from XML document. The Document refers to the HTML or XML document. The getDocumentElement() method provides the XML root Element. The getElementsByTag Name() provides the tag. Then get the value of node by getNodeValue().

Page 27: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

DOM Example1: Accessing XML file from Java

27

 

Page 28: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

28

Simple DOM program, I This program is adapted from CodeNotes® for XML

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); } }}

Page 29: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

29

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();

Page 30: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

30

Simple DOM program, III

The next step is to load in the XML file Here is the XML file, named hello.xml:

<?xml version="1.0"?> <display>Hello World!</display>

To read this file in, we add the following line to our program: Document document = builder.parse("hello.xml");

Notes: document contains the entire XML file (as a tree); it is the

Document Object Model If you run this from the command line, your XML file should be in

the same directory as your program An IDE may look in a different directory for your file; if you get a

java.io.FileNotFoundException, this is probably why

Page 31: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

31

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!

Page 32: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

32

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 If you have a lot of tree manipulation to do, DOM is

much more convenient than SAX If you don’t have a lot of tree manipulation to do,

consider using SAX instead

Page 33: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

33

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

Page 34: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

34

Preorder traversal in Java static void simplePreorderPrint(String indent, Node node) {

printNode(indent, node); if(node.hasChildNodes()) { Node child = node.getFirstChild(); while (child != null) { simplePreorderPrint(indent + " ", child); child = child.getNextSibling(); } } }

static void printNode(String indent, Node node) { System.out.print(indent); System.out.print(node.getNodeType() + " "); System.out.print(node.getNodeName() + " "); System.out.print(node.getNodeValue() + " "); printNamedNodeMap(node.getAttributes()); // see next slide System.out.println(); }

Page 35: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

35

Printing a NamedNodeMap private static void printNamedNodeMap(NamedNodeMap

attributes) throws DOMException { if (attributes != null) { for (int i = 0; i < attributes.getLength(); i++) { Node attribute = attributes.item(i); System.out.print(attribute.getNodeName() + "=" + attribute.getNodeValue()); } }}

Page 36: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

A Java-DOM Example

A stand-alone toy application BuildXml either creates a new db document with two person elements, or adds them to an existing db document

based on the example in Sect. 8.6 of Deitel et al: XML - How to program

Technical basis DOM support in Sun JAXP native XML document initialisation and storage

methods of the JAXP 1.1 default parser (Apache Crimson)

36

Page 37: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

Code of BuildXml (1)

Begin by importing necessary packages:

import java.io.*;import org.w3c.dom.*;import org.xml.sax.*;import javax.xml.parsers.*;// Native (parse and write) methods of the// JAXP 1.1 default parser (Apache Crimson):import org.apache.crimson.tree.XmlDocument;

37

Page 38: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

Code of BuildXml (2)

Class for modifying the document in file fileName:

public class BuildXml { private Document document;

public BuildXml(String fileName) { File docFile = new File(fileName); Element root = null; // doc root elemen

// Obtain a SAX-based parser: DocumentBuilderFactory factory =

DocumentBuilderFactory.newInstance();

38

Page 39: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

Code of BuildXml (3)

try { // to get a new DocumentBuilder: documentBuilder builder =

factory.newInstance();

if (!docFile.exists()) { //create new doc document = builder.newDocument();

// add a comment: Comment comment =

document.createComment( "A simple personnel

list"); document.appendChild(comment);

// Create the root element: root = document.createElement("db");

document.appendChild(root); 39

Page 40: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

Code of BuildXml (4)

… or if docFile already exists:

} else { // access an existing doc try { // to parse docFile

document = builder.parse(docFile);root = document.getDocumentElement();

} catch (SAXException se) {System.err.println("Error: " +

se.getMessage() );System.exit(1);

}

/* A similar catch for a possible IOException */

40

Page 41: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

Code of BuildXml (5)

Create and add two child elements to root:

Node personNode = createPersonNode(document, "1234",

"Pekka", "Kilpeläinen"); root.appendChild(personNode); personNode =

createPersonNode(document, "5678",

"Irma", "Könönen"); root.appendChild(personNode);

41

Page 42: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

Code of BuildXml (6)

Finally, store the result document:

try { // to write the // XML document to file

fileName ((XmlDocument) document).write(

new FileOutputStream(fileName));

} catch ( IOException ioe ) { ioe.printStackTrace(); }

42

Page 43: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

Subroutine to create person elements

public Node createPersonNode(Document document, String idNum, String fName, String lName)

{ Element person =

document.createElement("person"); person.setAttribute("idnum", idNum); Element firstName =

document. createElement("first");person.appendChild(firstName);

firstName. appendChild( document. createTextNode(fName) );

/* … similarly for a lastName */ return person;

} 43

Page 44: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

The main routine for BuildXml

public static void main(String args[]){ if (args.length > 0) {

String fileName = args[0]; BuildXml buildXml = new

BuildXml(fileName); } else {

System.err.println("Give filename as argument");

};} // main

44

Page 45: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

DOM Example2: Representing an XML Document as a JTree

45

Page 46: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

DOM Example: XMLTree

46

Page 47: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

DOM Example: XMLTree (continued)

47

Page 48: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

DOM Example: XMLTree (continued)

48

Page 49: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

DOM Example: XMLTree (continued)

49

Page 50: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

DOM Example: XMLTree (continued)

50

Page 51: SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul

More Examples..

http://www.mkyong.com/tutorials/java-xml-tutorials

http://courses.coreservlets.com/Course-Materials/java.html

http://www.corewebprogramming.com/

http://www.roseindia.net/xml/dom/accessing-xml-file-java.shtml

51