The DOM and the JAXP Packages

printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    

Java APIs for XML Kick Start
By Aoyon Chowdhury, Parag Choudhary

Table of Contents
Chapter 5.  The Document Object Model


The DOM APIs are developed and maintained by the W3C DOM Working Group. JAXP endorses the DOM Level 2 Core recommendations by including by reference the packages that contain the DOM APIs. JAXP also contains a package that enables the use of any DOM Level 2-compliant parser to be used in an application. You can learn more about DOM Level 2 specifications at http://www.w3.org/TR/DOM-Level-2-Core/.

The JAXP package for supporting DOM and the DOM APIs is defined in the following packages:

  • org.w3c.dom

  • javax.xml.parsers

The org.w3c.dom Package

The org.w3c.dom package contains the interfaces that define the DOM APIs. These interfaces represent the different node types supported by DOM specifications. These interfaces provide a number of methods with which you can access and work with different node types. The package also contains one exception. The following list describes the interfaces contained in this package:

  • Attr Represents an attribute of an Element object. This interface declares the methods with which you can get various information about an attribute and set its value. It is important to remember that while in an XML document, an attribute can have tokenized types, and the DOM specification treats all attribute values as strings.

  • CDATASection Represents the contents stored in the CDATA section of an XML document. The text contained in the CDATA section is available in the DOMString attribute of the Text node. This interface defines no methods.

  • CharacterData Declares the methods for accessing character data in the DOM. Note that there is no DOM object represented by CharacterData. This is because the actual representation of the character data for an element or attribute is done by the Text interface. However, the Text interface inherits the CharacterData interface to use the methods to work with the character data.

  • Comment Represents the contents of a comment in an XML document.

  • Document Represents the complete XML document. You can think of this interface as the root of the document tree, and the primary access point for all data in the document. It declares a number of create methods with which an application can create the different types of DOM nodes. This interface also defines a number of get methods with which an application can access the different nodes in a DOM.

  • DocumentFragment Represents a lightweight document. This interface is useful when you want to move or rearrange parts of a document as a result of user commands such as cut. An interesting feature of DocumentFragment is that when a DocumentFragment is inserted into a Document or a Node, it is the children of the DocumentFragment that are inserted, and not the DocumentFragment itself. Therefore, by using a DocumentFragment that contains multiple nodes, an application can use a single insert call to insert all the nodes of the DocumentFragment under a specific Node.

  • DocumentType Represents the doctype attribute of an XML document. This interface has methods with which you can access the public and system IDs of the DTD for the XML document, as well as the list of entities and notations defined for the document.

  • DOMImplementation Defines methods with which you can create a document of a specified doctype, create a DocumentType node, and query whether a DOM implementation implements a particular feature.

  • Element Declares methods with which you can access and modify the attributes of a specific element. This interface also declares methods with which the descendant elements can be accessed.

  • Entity Represents both parsed and unparsed entities in an XML document. It is important to remember that in the non-validating mode, some XML processors might not process entity declarations made in the external subset or declared in external parameter entities. When an entity is processed, the Entity node's child node will contain the replacement text. An Entity node and its child nodes are all read-only.

  • EntityReference Represents an entity reference in an XML document. Note that character references and references to predefined entities are expanded by the processor while building the DOM. This implies that the characters are represented by their Unicode equivalent rather than by an entity reference. It is also possible that instead of providing the EntityReference objects, the XML processor might instead completely expand references to entities while building the DOM.

  • NamedNodeMap Represents a collection of nodes that can be accessed by name or a number. However, it is important to remember that the objects in this collection are not stored in any particular order.

  • Node Represents a single node in the document tree, and is the primary data type for the DOM. The Node interface provides methods with which an application can determine the node types and their values and attributes. This interface also provides the methods that enable the traversal across the DOM tree, and to add and remove nodes from the DOM. All interfaces, except for DOMImplementation, NamedNodeMap, and NodeList, inherit the Node interface.

  • NodeList Provides the abstraction of an ordered collection of nodes. However, this interface does not define or constrain how this collection is implemented. This interface declares two methods with which the number of nodes in a node list can be determined, and a specific Node from the node list can be accessed.

    You will see later in the chapter that the Node and the NodeList interfaces are closely coupled. For example, the getChildNodes() method of the Node interface returns a NodeList containing all the children of the Node. The item(int index) method of the NodeList interface can then be used to access a specific Node.

  • Notation Provides methods with which the public ID and the system ID of the notation can be accessed. The Notation node is a read-only node. A notation is a mechanism by which an unparsed entity is declared in an XML document. To find out more about notations and unparsed entities, see the section "Handling Unparsed Entities" in Chapter 4, "Advanced Use of SAX."

  • ProcessingInstruction Declares three methods with which the target of the processing instruction can be accessed, and by which the data for the processing instruction can be accessed or set.

  • Text Represents the character data of an element or the value of an attribute. As mentioned earlier, this interface inherits from the CharacterData interface, along with the Node interface.

The org.w3c.dom package contains the following exception:

  • DOMException A DOMException is raised when it is impossible to perform an operation. This could be a result of logical reasons such as loss of data, or physical reasons, such as when the installation itself becomes unstable.

    For example, as per the DOM specification, a text node cannot have child nodes. If an application tries to insert a child node under the text node, a DOMException is thrown. This is a type of logical error.

The javax.xml.parsers Package

The javax.xml.parsers package contains the classes that enable an application to process the XML documents using either SAX or DOM parsers.

The classes contained in this package that support DOM are as follows:

  • DocumentBuilder This class defines the API to get the DOM Document object from the XML document being parsed. A Document can be thought of as the root of a document tree, which provides access to the document's data.

  • DocumentBuilderFactory This abstract class defines a factory API that enables applications to obtain a parser using the DocumentBuilder class. The parser can then produce DOM object trees from an XML document.


printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    
Top

[0672324342/ch05lev1sec2]

 
 


JavaT APIs for XML Kick Start
JAX: Java APIs for XML Kick Start
ISBN: 0672324342
EAN: 2147483647
Year: 2002
Pages: 133

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net