DOMNewDoc.java - Building a New DOM Document

This section provides a tutorial example on how to write a program to create a new DOM document object with DOM classes and methods like: DocumentBuilder.newDocument(), Document.createElement(), Document.createTextNode(), and Node.appendChild().

DOM documents can also be created from scratch instead of parsed from an XML file. This can be easily done by using the following methods:

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

/* DOMNewDoc.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
 */
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+" ");
      }
   }
}

Here is the output with JDK 9 or higher:

herong> java DOMNewDoc.java

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

Notice that an attribute is represented twice in the DOM tree.

The same program produces different output with JDK 1.8:

herong> \progra~1\java\jdk1.8.0\bin\java DOMNewDoc

#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

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