A.2. DOM Level 2DOM 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.domThis 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.1. AttrThis interface represents an XML attribute (on an element) within Java. It provides access to the name and value of the attribute, and allows the setting of the value (for mutability).[29] The getSpecified( ) method indicates if the attribute (and its value) was explicitly noted in the XML document, or if a value was not specified but the document's DTD assigned a default value to the attribute. Finally, the "owning" element can be obtained from this interface.
A.2.1.2. CDATASectionThis interface does not define any methods of its own; instead it inherits all of the Text interface's methods. However, by having its own interface (and thus its own node type), a distinction can be drawn between text within XML CDATA sections and simple text (not in a CDATA section) within an element.
A.2.1.3. CharacterDataThis interface is the "super" interface for all textual Node types in DOM (Text, Comment, and indirectly CDATASection). It defines methods for accessing and setting the data within a textual node, as well as a set of methods for dealing with the textual data directly as characters: obtaining the length, appending, inserting, and deleting data, and replacing all or part of the data. All of these methods throw DOMExceptions when the node is read-only.
A.2.1.4. CommentThis interface provides a Java representation for an XML comment. Similar to CDATASection, it adds no methods of its own but does allow a distinction (based on the type of the interface) to distinguish between text and comments in an XML document.
A.2.1.5. DocumentThis 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.
A.2.1.6. DocumentFragmentThis interface provides for dealing with only a portion of a complete Document object at one time. It is useful for manipulating portions of a DOM tree without having to store the entire tree in memory.
A.2.1.7. DocumentTypeThis interface represents an XML document's DOCTYPE declaration. The name is the element name immediately following <!DOCTYPE, and the system ID and public ID of any referenced DTD are available as well. Additionally, if any inline entities or notations are present, they can be obtained through the appropriate getXXX( ) methods.
A.2.1.8. DOMExceptionThis class provides an Exception for DOM interfaces to throw when problems occur. It also provides a set of error codes that represent the various problems that occur using DOM and might result in the Exception being thrown.
A.2.1.9. DOMImplementationThis 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.
A.2.1.10. ElementThis interface provides a Java representation of an XML element. It provides methods to get its name and attributes, as well as to set these values. It also supplies several flavors of access to the XML attributes, including namespace-aware versions of the getXXX( ) and setXXX( ) methods.
A.2.1.11. EntityThis provides a Java representation of an entity (parsed or unparsed) in an XML document. Access to the system ID and public ID as well as the notation for the entity (from the DTD) is provided through accessor methods.
A.2.1.12. EntityReferenceThis interface represents the resulting value from an entity reference once the entity has been resolved. This interface assumes that character and predefined entity references have already occurred when this interface is exposed to the application client.
A.2.1.13. NamedNodeMapThis interface defines a list, much like NodeList, but requires that each Node in the list be a named Node (such as an Element or Attr). Because of this requirement, methods can be provided to access members of the list by their names (with or without namespace support). The list also provides for removal and modification of its members. These methods all throw DOMExceptions when the referenced Node is read-only.
A.2.1.14. NodeThis 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( ).
A.2.1.15. NodeListThis interface is a DOM structure analogous to a Java Vector or List. It is the return value of any method that supports multiple Node implementations as a result. This allows iteration through the items as well as providing the ability to get a Node at a specific index.
A.2.1.16. NotationThis interface represents a NOTATION construct in a DTD, used to declare the format of an unparsed entity or for declaration of PIs. This provides access to both the system ID and public ID within the declaration. Both return null if they are not present.
A.2.1.17. ProcessingInstructionThis interface represents an XML processing instruction (PI). It provides methods for getting the target and the data of the PI. Note that there is no means of accessing the "name/value" pairs within the PI individually. The data can also be set for the PI.
A.2.1.18. TextThis interface provides a Java representation of an XML element's textual data. The only method it adds to those defined in CharacterData is one that will split the node into two nodes. The original Text node contains text up to the specified offset, and the method returns a new Text node with the text after the offset. Like other mutability methods, a DOMException is thrown when the node is read-only.
Copyright © 2002 O'Reilly & Associates. All rights reserved. |
|
|