Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations

Presentation on theme: "SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) 2011-12/Spring, Bahçeşehir University, Istanbul."— Presentation transcript:

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

2 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 s. 2

3 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

4 javax.xml.parsers DOM DocumentBuilder, DocumentBuilderFactory SAX SAXParser, SAXParserFactory General FactoryConfigurationError ParserConfigurationException

5 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”);

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

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

8 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

9 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

10 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

11 DOM Installation and Setup (JDK 1.3) 11

12 DOM Installation and Setup (JDK 1.3) 12

13 DOM Installation and Setup (JDK 1.3) 13

14 Steps for DOM Parsing 14

15 Step 1: Specifying a Parser 15

16 Step 1: Example 16

17 Step 2: Create a JAXP Document Builder 17

18 Step3: Invoke the Parser to Create a Document 18

19 Step 4: Normalize the Tree 19

20 Step 5: Obtain the Root Node of the Tree 20

21 Step 6: Examine and Modify Properties of the Node 21

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

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

24 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.xml 2)

25 DOM Example1: Accessing XML file from Java 25

26 DOM Example1: Accessing XML file from Java 26 2) 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().

27 DOM Example1: Accessing XML file from Java 27

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

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

30 30 Simple DOM program, III The next step is to load in the XML file Here is the XML file, named hello.xml : Hello World! 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, this is probably why

31 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!

32 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

33 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

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

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

36 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

37 Code of BuildXml (1) Begin by importing necessary packages: import*; 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

38 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

39 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

40 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

41 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

42 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

43 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

44 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

45 DOM Example2: Representing an XML Document as a JTree 45

46 DOM Example: XMLTree 46

47 DOM Example: XMLTree (continued) 47

48 DOM Example: XMLTree (continued) 48

49 DOM Example: XMLTree (continued) 49

50 DOM Example: XMLTree (continued) 50

51 More Examples.. Materials/java.html java.shtml 51

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

Similar presentations

Ads by Google