1. Introduction to dom4j
Dom4j is an open source XML parsing package produced by dom4j.org. The biggest feature of dom4j is the use of a large number of interfaces, which is also considered to be more flexible than JDOM. Its main interfaces are defined in the org. dom4j package:
| Attribute |
Defines XML attributes |
| Branch |
Branch defines a common behavior for nodes that can contain sub-nodes, such as XML elements and documents. |
| CDATA |
Defines the xml cdata Region |
| Characterdata |
Characterdata is an excuse to identify character-based nodes. Such as CDATA, comment, text. |
| Comment |
Comment defines the XML annotation Behavior |
| Document |
Defines XML documents |
| Documenttype |
Documenttype defines XML doctype Declaration |
| Element |
Element defines XML elements |
| Elementhandler |
Elementhandler defines the processor of the Element Object |
| Elementpath |
Used by elementhandler to obtain the path level information currently being processed |
| Entity |
Entity defines XML Entity |
| Node |
Node defines polymorphism for all XML nodes in dom4j. |
| Nodefilter |
Nodefilter defines the behavior of a filter or predicate generated in the dom4j node (predicate) |
| Processinginstruction |
Processinginstruction defines XML processing instructions. |
| Text |
Text defines XML text nodes. |
| Visitor |
Visitor is used to implement the visitor mode. |
| Xpath |
After analyzing a string, XPath provides an XPATH expression. |
The inheritance relationships between interfaces are as follows:
- Interface java. Lang. cloneable
- Interface org. dom4j. Node
- Interface org. dom4j. Attribute
- Interface org. dom4j. Branch
- Interface org. dom4j. Document
- Interface org. dom4j. Element
- Interface org. dom4j. characterdata
- Interface org. dom4j. CDATA
- Interface org. dom4j. Comment
- Interface org. dom4j. Text
- Interface org. dom4j. documenttype
- Interface org. dom4j. Entity
- Interface org. dom4j. processinginstruction
Ii. Example
1. Read and parse XML documents
Reading and Writing XML documents mainly depends on the org. dom4j. Io package, which provides two different methods: domreader and saxreader, and the call method is the same.
// Read XML from the file, input the file name, and return the XML file
Public document read (string filename) throws malformedurlexception, using entexception {
Saxreader reader = new saxreader ();
Document document = reader. Read (new file (filename ));
Return document;
}
The reader's read method is overloaded and can be read from multiple sources such as inputstream, file, and URL. The resulting document object represents the entire XML. Note that the character encoding is converted according to the encoding defined in the XML file header.
2. Obtain the root node
All XML analysis starts with the root element.
Public element getrootelement (document DOC ){
Return Doc. getrootelement ();
}
3. Traverse the XML tree
Dom4j provides at least three methods to traverse nodes:
1) iterator)
// Enumerate all subnodes
For(Iterator I = root. elementiterator (); I. hasnext ();){
Element element = (element) I. Next ();
// Do something
}
// Enumerate nodes named foo
For(Iterator I = root. elementiterator (FOO); I. hasnext ();){
Element Foo = (element) I. Next ();
// Do something
}
// Enumeration attribute
For(Iterator I = root. attributeiterator (); I. hasnext ();){
Attribute attribute = (attribute) I. Next ();
// Do something
}
2) Recursion
Public void treewalk (){
Treewalk (getrootelement ());
}
Public void treewalk (element ){
For (INT I = 0, size = element. nodecount (); I <size; I ++ ){
Node node = element. node (I );
If (node instanceof element ){
Treewalk (element) node );
} Else {// do something ....
}
}
}
3) Visitor Mode
Visitor is one of the gof design patterns. The main principle is that the two types retain each other's references, and one serves as the visitor to access many visitable. You only need to customize a class to implement the visitor interface.
Public class myvisitor extends visitorsupport {
Public void visit (element ){
System. Out. println (element. getname ());
}
Public void visit (attribute ATTR ){
System. Out. println (ATTR. getname ());
}
}
Call: Root. Accept (New myvisitor ())