- 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:

 - Copyright (c) 2014,, All Rights Reserved.
import javax.xml.parsers.*;
import org.w3c.dom.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
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"); 
         Element w = d.createElement("word");
         Element e = d.createElement("update");
         e = d.createElement("name");
         e = d.createElement("definition");
         e.appendChild(d.createTextNode("Document Type Definition"));
         TransformerFactory tf = TransformerFactory.newInstance();
         Transformer m = tf.newTransformer();
         DOMSource source = new DOMSource(d);
         StreamResult result = new StreamResult(x);
         m.transform(source, result);
      } catch (ParserConfigurationException e) {
      } catch (TransformerConfigurationException e) {
      } catch (TransformerException e) {

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

This tells us that the factory pickup an implementation of Transformer from the 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>

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.

Last update: 2014.

Table of Contents

 About This JDK Tutorial Book

 Downloading and Installing JDK 1.8.0 on Windows

 Downloading and Installing JDK 1.7.0 on Windows

 Downloading and Installing JDK 1.6.2 on Windows

 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

 Socket Network Communication

 Datagram Network Communication

DOM (Document Object Model) - API for XML Files - Parsing XML Files with DOM - Browsing DOM Tree Structure - Building a New DOM Document - 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 - Secret Key Encryption and Decryption

 The SSL (Secure Socket Layer) Protocol

 SSL Socket Communication Testing Programs

 SSL Client Authentication

 HTTPS (Hypertext Transfer Protocol Secure)

 Outdated Tutorials


 PDF Printing Version