Building a New DOM Document Object

This section provides a tutorial example on how to build a new DOM Document object and print it out as DOM Node tree.

DOM document objects 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 object with these methods:

 - Copyright (c) 2002-2018 All Rights Reserved.
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");
         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"));
         printNode(d, "");
      } catch (ParserConfigurationException e) {
   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+" ");

Run this sample Java program in JDK, you should get:

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