Compared with the use of Dom, sax, and JAXP mechanisms to parse XML, dom4j is superior in performance, powerful in functionality, and extremely easy to use. As long as you understand the basic concepts of Dom, you can use the dom4j API documentation to parse XML. Dom4j is an open-source API. In actual projects, dom4j is often used as a tool for parsing XML.
Let's take a look at the inheritance relationships established by the DOM tree corresponding to XML in dom4j.
For the XML standard definition that corresponds to the content listed in Figure 2-1, dom4j provides the following implementation:
In addition, the nodetype enumeration of dom4j implements the node type defined in the XML specification. In this way, you can use constants to determine the node type when traversing XML documents.
Common APIs
Class org. dom4j. Io. saxreader
- Read provides multiple ways to read XML files and returns a domcument object.
Interface org. dom4j. Document
- Iterator uses this method to obtain the node
- Getrootelement: Get the root node
Interface org. dom4j. Node
- Getname: Get the node name. For example, get the root node name as bookstore.
- Getnodetype: Get the constant value of the node type. For example, get the bookstore type to 1 -- element.
- Getnodetypename: Get the node type name. For example, the obtained bookstore type name is element.
Interface org. dom4j. Element
- Attributes returns the attribute list of this element
- Attributevalue obtains the attribute value based on the passed attribute name.
- Elementiterator returns the iterator containing child elements
- Elements returns a list containing child elements.
Interface org. dom4j. Attribute
Interface org. dom4j. Text
Interface org. dom4j. CDATA
- Gettext: Get the CDATA section Value
Interface org. dom4j. Comment