DOMToXML.java - Converting DOM Documents to XML Files

This section provides a tutorial example on how to write a program to convert a DOM document object into an XML file using the default XML transformer, org.apache.xalan.transformer, provided in the JDK.

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) HerongYang.com. All Rights Reserved.
 */
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:

com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl@52e922

This tells us that the factory pickup an implementation of Transformer from the com.sun.org.apache.xalan.internal.xsltc.trax 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 manually to be able to fit the format of this book.

Table of Contents

 About This JDK Tutorial Book

 JDK (Java Development Kit)

 Java Date-Time API

 Date, Time and Calendar Classes

 Date and Time Object and String Conversion

 Number Object and Numeric String Conversion

 Locales, Localization Methods and Resource Bundles

 Calling and Importing Classes Defined in Unnamed Packages

 HashSet, Vector, HashMap and Collection Classes

 Character Set Encoding Classes and Methods

 Character Set Encoding Maps

 Encoding Conversion Programs for Encoded Text Files

 Java Logging

 Socket Network Communication

 Datagram Network Communication

DOM (Document Object Model) - API for XML Files

 DOMParser.java - Parsing XML Files with DOM

 DOMBrowser.java - Browsing DOM Tree Structure

 DOMNewDoc.java - Building a New DOM Document

DOMToXML.java - Converting DOM Documents to XML Files

 SAX (Simple API for XML)

 DTD (Document Type Definition) - XML Validation

 XSD (XML Schema Definition) - XML Validation

 XSL (Extensible Stylesheet Language)

 Message Digest Algorithm Implementations in JDK

 Private key and Public Key Pair Generation

 PKCS#8/X.509 Private/Public Encoding Standards

 Digital Signature Algorithm and Sample Program

 "keytool" Commands and "keystore" Files

 KeyStore and Certificate Classes

 Secret Key Generation and Management

 Cipher - Encryption and Decryption

 The SSL (Secure Socket Layer) Protocol

 SSL Socket Communication Testing Programs

 SSL Client Authentication

 HTTPS (Hypertext Transfer Protocol Secure)

 Outdated Tutorials

 References

 Full Version in PDF/EPUB