Converting DOM Document Objects to XML Files

This section provides a tutorial example on how to convert DOM document objects into XML files using the XML transform package provided in JDK.

Once you have a DOM document object 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 JDK.

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 Java program to illustrate how to use Transformer to convert a DOM document into a XML file:

 * Copyright (c) 2002-2018 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 JDK 10 and 1.8, you will get the following on the Java console window:

herong> java DOMToXML word.xml

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="2014-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 included in this book.

Table of Contents

 About This Book

 Introduction of XML (eXtensible Markup Language)

 XML File Syntax

 XML File Browsers

 XML-JSON Document Conversion

DOM (Document Object Model) Programming Interface

 What Is DOM (Document Object Model)

 Using DOM Implementation Provided in JDK

 DOM Specifications and DOM Node Interface - DOM Interface Java Example

 XML DOM Node Tree Example

 Building a New DOM Document Object

Converting DOM Document Objects to XML Files

 SAX (Simple API for XML) Programming Interface

 DTD (Document Type Definition) Introduction

 Syntaxes of DTD Statements

 Validating an XML Document against the Specified DTD Document Type

 XSD (XML Schema Definition) Introduction

 Syntaxes of XSD Statements

 Validating XML Documents Against Specified XML Schemas

 XSL (Extensible Stylesheet Language) Introduction

 Java Implementation of XSLT

 XSLT (XSL Transformations) Introduction

 XPath (XML Path) Language

 XSLT Elements as Programming Statements

 Control and Generate XML Element in the Result

 PHP Extensions for XML Manipulation

 Processing XML with Python Scripts

 XML Notepad - XML Editor

 XML Tools Plugin for Notepad++

 XML Plugin Packages for Atom Editor

 XML 1.1 Changes and Parsing Examples

 Archived Tutorials


 Full Version in PDF/EPUB