The DOMImplementation interface, shown in Example 10.1, is an abstract factory that is responsible for creating two thingsnew Document and DocumentType objects. It also provides the hasFeature() method discussed in Chapter 9 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 , the following chunk of code creates a new DocumentType object named svgDOCTYPE pointing to the Scalable Vector Graphics (SVG) DTD:

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

If the DTD does not have a public ID, you can simply 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 document type declarations or namespace URIs. If the document is merely well- formed , then you simply can pass null for the doctype argument. If the document root element is not in a namespace, you also can 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 document type 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 use the methods of the Node interface to add these newly created nodes to the tree.

Processing XML with Java. A Guide to SAX, DOM, JDOM, JAXP, and TrAX
Processing XML with Javaв„ў: A Guide to SAX, DOM, JDOM, JAXP, and TrAX
ISBN: 0201771861
EAN: 2147483647
Year: 2001
Pages: 191 © 2008-2017.
If you may any questions please contact us: