home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Book HomeJava and XML, 2nd EditionSearch this book

A.2. DOM Level 2

DOM provides a complete, in-memory representation of an XML document. Developed by the W3C, DOM provides detail about the structure of a document after it has been completely parsed. While DOM Level 3 will specify an API for getting the DOM Document object, there is currently nothing in DOM that defines this behavior. Like SAX, most of the core DOM package is made up of interfaces that define structures within an XML document, and map those structures to the Java language (these same mappings apply to CORBA, JavaScript, and other languages as well).

A.2.1. Package: org.w3c.dom

This package contains the core interfaces and classes for DOM Level 2. Typically a vendor's parsing software provides an implementation of those interfaces that are implicitly used by your application software.

A.2.1.5. Document

This interface is the DOM representation of a complete XML document. It is also the key for creating new XML elements, attributes, PIs, and other constructs. In addition to allowing retrieval of the DTD declaration (getDocType( )) and root element (getDocumentElement( )), this allows searching through the tree in a pre-order fashion for a specific element (getElementsByTagName( )). Because the DOM model requires that all Node implementations be tied to a DOM Document object, this provides methods for creating the various types of DOM Nodes. Each createXXX( ) method has a complement that supports namespaces through createXXXNS( ). Additionally, Nodes can be imported into this Document through importNode( ); the boolean value indicates if the children of the imported Node should be recursively imported as well.

public interface Document extends Node {
    public DocumentType getDoctype( );
    public DOMImplementation getImplementation( );
    public Element getDocumentElement( );
    public Element createElement(String tagName) throws DOMException;
    public DocumentFragment createDocumentFragment( );
    public Text createTextNode(String data);
    public Comment createComment(String data);
    public CDATASection createCDATASection(String data)
        throws DOMException;
    public ProcessingInstruction 
        createProcessingInstruction(String target, String data)
        throws DOMException;
    public Attr createAttribute(String name) throws DOMException;
    public EntityReference createEntityReference(String name)
        throws DOMException;
    public NodeList getElementsByTagName(String tagname);
    public Node importNode(Node importedNode, boolean deep)
        throws DOMException;
    public Element createElementNS(String namespaceURI, 
                                   String qualifiedName)
        throws DOMException;
    public Attr createAttributeNS(String namespaceURI, 
                                  String qualifiedName)
        throws DOMException;
    public NodeList getElementsByTagNameNS(String namespaceURI, 
                                           String localName);
    public Element getElementById(String elementId);
}

A.2.1.9. DOMImplementation

This interface attempts to provide a standard entry point for accessing vendor- specific DOM implementations, and allowing the creation of a DocumentType and Document within those vendor implementations.[30] It also provides a method (hasFeature( )) for querying the implementation for a specific feature support, like the DOM Level 2 Traversal or Range modules.

[30]Unfortunately, to obtain an instance of a DOMImplementation, you must have a Document object and use getDOMImplementation( ), or directly load the vendor's classes. This tends to result in a chicken-and-egg scenario; see Chapter 5, "DOM" and Chapter 6, "Advanced DOM" for more details.

public interface DOMImplementation {
    public boolean hasFeature(String feature, String version);
    public DocumentType createDocumentType(String qualifiedName, 
                                           String publicId, 
                                           String systemId)
        throws DOMException;
    public Document createDocument(String namespaceURI, 
                                   String qualifiedName, 
                                   DocumentType doctype)
        throws DOMException;
}

A.2.1.14. Node

This is the central interface for all DOM objects. It provides a robust set of methods for accessing information about a Node in the DOM tree. It also allows for handling of a Node's children (if they exist). While most of the methods are self-explanatory, there are several methods worth noting: getAttributes( ) only returns non-null data if the Node is an Element; cloneNode( ) provides for a shallow or deep copy of a Node; normalize( ) moves all text into non-adjacent Text nodes (no two Text nodes are adjacent, and all resolved textual entity references are consolidated into Text nodes); and isSupported( ) provides information about the feature set of the Node. Namespace-aware methods are also provided (getNamespaceURI( ), getPrefix( ), and getLocalName( )). Finally, a set of constants is provided for identifying the type of a Node by comparing the constants against the result of getNodeType( ).

public interface Node {
    public String getNodeName( );
    public String getNodeValue( ) throws DOMException;
    public void setNodeValue(String nodeValue) throws DOMException;
    public short getNodeType( );
    public Node getParentNode( );
    public NodeList getChildNodes( );
    public Node getFirstChild( );
    public Node getLastChild( );
    public Node getPreviousSibling( );
    public Node getNextSibling( );
    public NamedNodeMap getAttributes( );
    public Document getOwnerDocument( );
    public Node insertBefore(Node newChild,  Node refChild)
        throws DOMException;
    public Node replaceChild(Node newChild, Node oldChild)
                             throws DOMException;
    public Node removeChild(Node oldChild) throws DOMException;
    public Node appendChild(Node newChild) throws DOMException;
    public boolean hasChildNodes( );
    public Node cloneNode(boolean deep);
    public void normalize( );
    public boolean isSupported(String feature, String version);
    public String getNamespaceURI( );
    public String getPrefix( );
    public void setPrefix(String prefix) throws DOMException;
    public String getLocalName( );
    public boolean hasAttributes( );

    // Node Type Constants
    public static final short ELEMENT_NODE;
    public static final short ATTRIBUTE_NODE;
    public static final short TEXT_NODE;
    public static final short CDATA_SECTION_NODE;
    public static final short ENTITY_REFERENCE_NODE;
    public static final short ENTITY_NODE;
    public static final short PROCESSING_INSTRUCTION_NODE;
    public static final short COMMENT_NODE;
    public static final short DOCUMENT_NODE;
    public static final short DOCUMENT_TYPE_NODE;
    public static final short DOCUMENT_FRAGMENT_NODE;
    public static final short NOTATION_NODE;
}


Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.