Chapter 10. Creating XML Documents with DOM

Table of Contents

Locating a DOMImplementation
Implementation Specific Class
JAXP DocumentBuilder
DOM3 DOMImplementationRegistry
The Document Interface as an Abstract Factory
The Document Interface as a Node Type
Getter methods
Finding elements
Transferring nodes between documents

DOM is a read-write API. DOM documents are created not only by parsing text files, but also by creating new documents in memory out of nothing at all. These documents can then be serialized onto a stream or into a file. The abstract factory interface that creates new Document objects is called DOMImplementation. The Document interface has a dual purpose. First it represents XML documents themselves and provides access to their contents, document type declaration, and other properties. Secondly it too is an abstract factory responsible for creating the nodes that go in the document: elements, text, comments, processing instructions, etc. Each such node belongs exclusively to the document that created it and cannot be moved to a different document.


The DOMImplementation interface, shown in Example 10.1, is an abstract factory that is responsible for creating two things, new Document and DocumentType objects. It also provides the hasFeature() method discussed in the last chapter that tells you what features this implementation supports.

Example 10.1. The DOMImplementation interface

package org.w3c.dom;

public interface DOMImplementation {
  public DocumentType createDocumentType(
   String rootElementQualifiedName, 
   String publicID, String systemID) throws DOMException;
  public Document createDocument(String rootElementNamespaceURI,
   String rootElementQualifiedName, DocumentType doctype) 
   throws DOMException;
  public boolean hasFeature(String feature, String version);


For example, given a DOMImplementation object named impl, this chunk of code creates a new DocumentType object named svgDOCTYPE pointing to the Scalable Vector Graphics DTD:

DocumentType svgDOCTYPE = impl.createDocumentType("svg", 
 "-//W3C//DTD SVG 1.0//EN", 

If the DTD does not have a public ID, you can just pass null for the second argument.

You can use this DocumentType object when constructing a new SVG Document object:

Document svgDoc = impl.createDocument(
 "", "svg", svgDOCTYPE

If svgDoc were serialized into a text file, it would look something like this (modulo insignificant white space):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" 
<svg xmlns=""/>

Of course not all XML documents have DOCTYPE declarations or namespace URIs. If the document is merely well-formed, then you can just pass null for the doctype argument. If the document root element is not in a namespace, you can also pass null for the namespace URI. This code fragment creates an XML-RPC document with neither a document type declaration nor a namespace URI:

Document xmlrpc = impl.createDocument(null, "methodCall", null);

These Document objects, with or without DOCTYPE declarations, are not yet complete. In particular, they do not yet have any content beyond an empty root element. For that, you’ll have to use the methods of the Document interface to create nodes, and the methods of the Node interface to add these newly created nodes to the tree.

Copyright 2001, 2002 Elliotte Rusty Haroldelharo@metalab.unc.eduLast Modified January 27, 2002
Up To Cafe con Leche