Processing XML with Javaв„ў: A Guide to SAX, DOM, JDOM, JAXP, and TrAX

JDOM models an XML document as a Document object that contains a List of Comment and ProcessingInstruction objects and a single Element object for the root element.

Each Element object contains a List of its children: Comment , ProcessingInstruction , Text , and other Element objects. In addition, it has separate lists of attributes and additional namespaces. The list of children is accessible through the getContent() method. Content can be appended to an element or to a document with the heavily overloaded addContent() method, or removed from the tree using the heavily overloaded removeContent() method. Other mutation operations, such as inserting a node at the beginning or in the middle of an element's children, require using the methods of java.util.List .

With the exception of Namespace , the JDOM core node classes have public constructors, and they are constructed in about the most obvious ways you can imagine. For example, to create a new Attribute object that represents the attribute revision="20020430" , you would use the constructor Attribute("revision", "20020430") . Namespaces and local names are always separated in argument lists. Sometimes namespaces are provided as one or two strings containing the namespace URI and the optional prefix. At other times they're provided as a combined Namespace object. (JDOM is not perfectly consistent here.)

All of the core node classes have the getter and setter methods you would expectfor example, setTarget() , getTarget() , setData() , and getData() for ProcessingInstruction ; setName() , getName() , setNamespace() , and getNamespace() for Element ; and so forth. In addition there are a few common methods that show up across several classes. The detach() method removes a node from its parent. The getParent() method returns a node's parent element. And all of the JDOM nodes provide the usual Java utility methods: equals() , hashCode() , and toString() . Finally, all of these classes implement both Serializable and Cloneable , and all override clone() .

Категории