JDK Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 4.32, 2006

Document Object Model (DOM)

Part:   1  2  3  

JDK Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Internationalization

Character Set and Encoding

Socket Communication

Document Object Model (DOM)

XSD Validation in Java

XSL - Transformer in Java

JCA - Private and Public Key Pairs

JCE - Secret Key

SSL (Secure Socket Layer)

SSL - Client Authentication

... Table of Contents

(Continued from previous part...)

The following program shows how to create a simple DOM document with these methods:

/**
 * DOMNewDoc.java
 * Copyright (c) 2002 by Dr. Herong Yang
 */
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
class DOMNewDoc {
   public static void main(String[] args) {
      try {
         DocumentBuilderFactory f 
            = DocumentBuilderFactory.newInstance();
         DocumentBuilder b = f.newDocumentBuilder();
         Document d = b.newDocument();
         Element r = d.createElement("dictionary"); 
         d.appendChild(r);
         Element w = d.createElement("word");
         r.appendChild(w);
         Element e = d.createElement("update");
         w.appendChild(e);
         e.setAttribute("date","2002-12-24");
         e = d.createElement("name");
         w.appendChild(e);
         e.setAttribute("is_acronym","true");
         e.appendChild(d.createTextNode("DTD"));
         e = d.createElement("definition");
         w.appendChild(e);
         e.appendChild(d.createTextNode("Document Type Definition"));
         printNode(d, "");
      } catch (ParserConfigurationException e) {
         System.out.println(e.toString()); 	
      }
   }
   static void printNode(Node n, String p) {
      NodeList l = n.getChildNodes();
      NamedNodeMap m = n.getAttributes();
      int ml = -1;
      if (m!=null) ml = m.getLength(); 
      System.out.println(p+n.getNodeName()+": "+n.getNodeType()+", "
         +l.getLength()+", "+ml+", "+n.getNodeValue());
      for (int i=0; i<ml; i++) {
         Node c = m.item(i);
         printNode(c,p+" |-");
      }
      for (int i=0; i<l.getLength(); i++) {
         Node c = l.item(i);
         printNode(c,p+" ");
      }
   }
}

Output:

#document: 9, 1, -1, null
 dictionary: 1, 1, 0, null
  word: 1, 3, 0, null
   update: 1, 0, 1, null
    |-date: 2, 0, -1, 2002-12-24
   name: 1, 1, 1, null
    |-is_acronym: 2, 0, -1, true
    #text: 3, 0, -1, DTD
   definition: 1, 1, 0, null
    #text: 3, 0, -1, Document Type Definition

Converting DOM Documents to XML Files

Once you have a DOM document in memory, for sure you want to know how to write it out into an XML file. One way to do this is to use the XML transform package offered in J2SDK.

The main class in the XML transform package is called Transformer, which can be used process XML from a variety of sources and write the transformation output to a variety of sinks.

Here is a program to illustrate how to use Transformer to convert a DOM document into a XML file:

/**
 * DOMToXML.java
 * Copyright (c) 2002 by Dr. Herong Yang
 */
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
class DOMToXML {
   public static void main(String[] args) {
      try {
      	 File x = new File(args[0]);
         DocumentBuilderFactory f 
            = DocumentBuilderFactory.newInstance();
         DocumentBuilder b = f.newDocumentBuilder();
         Document d = b.newDocument();
         Element r = d.createElement("dictionary"); 
         d.appendChild(r);
         Element w = d.createElement("word");
         r.appendChild(w);
         Element e = d.createElement("update");
         w.appendChild(e);
         e.setAttribute("date","2002-12-24");
         e = d.createElement("name");
         w.appendChild(e);
         e.setAttribute("is_acronym","true");
         e.appendChild(d.createTextNode("DTD"));
         e = d.createElement("definition");
         w.appendChild(e);
         e.appendChild(d.createTextNode("Document Type Definition"));
         TransformerFactory tf = TransformerFactory.newInstance();
         Transformer m = tf.newTransformer();
         System.out.println(m.toString()); 	
         DOMSource source = new DOMSource(d);
         StreamResult result = new StreamResult(x);
         m.transform(source, result);
      } catch (ParserConfigurationException e) {
         System.out.println(e.toString()); 	
      } catch (TransformerConfigurationException e) {
         System.out.println(e.toString()); 	
      } catch (TransformerException e) {
         System.out.println(e.toString()); 	
      }
   }
}

Run this program with "java DOMToXML word.xml, you will get the following on the Java console window:

org.apache.xalan.transformer.TransformerIdentityImpl@743399

This tells us that the factory pickup an implementation of Transformer from the org.apache.xalan.transformer package.

word.xml, the XML file generated by the program, looks very good:

<?xml version="1.0" encoding="UTF-8"?>
<dictionary><word><update date="2002-12-24"/>
<name is_acronym="true">DTD</name>
<definition>Document Type Definition</definition></word>
</dictionary>

Note that entire root element is stored in a single line. It was split into multiple lines to be included in here.

Source: Herong's Notes on XML.

Part:   1  2  3  

Dr. Herong Yang, updated in 2006
JDK Tutorials - Herong's Tutorial Notes - Document Object Model (DOM)