Dom4j is a Java xml api, similar to JDOM, used to read and write XML files. Dom4j is a very good Java xml api with excellent performance, powerful functionality, and extreme ease of use. It is also an open source software, more and more Java software are using dom4j to read and write XML. It is particularly worth mentioning that Sun's jaxm is also using dom4j. This is a required jar package.
As mentioned above, dom4j is so good and easy to use. We will share with you some usage of dom4j from today on.
The main interfaces of dom4j are defined in the org. dom4j package:
Attribute |
Attribute defines XML attributes. |
Branch |
Branch defines a common behavior for nodes that can contain sub-nodes, such as XML elements and documents, |
CDATA |
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. |
To understand this interface, you must understand the inheritance relationship of the interface:
- 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
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. This is the benefit of relying on interfaces.
[Java]View Plain Copy
- // 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;
- }
Reader's read method is overloaded and can be read through different parameters such as inputstream, file, and URL. The resulting document object contains the entire XML table.
According to my own experience, the character encoding read is converted according to the encoding defined in the XML file header. In case of garbled characters, make sure that the names of all codes are consistent.
In the following example, the saxreader class reads XML files through inputstream:
XML file to be read:
[HTML]View Plain Copy
- <? XML version = "1.0" encoding = "UTF-8"?>
- <Config>
- <DB-Info>
- <Driver-Name> oracle. JDBC. Driver. oracledriver </driver-Name>
- <URL> JDBC: oracle: thin: @ localhost: 1522: mydb </URL>
- <User-Name> DRP </user-Name>
- <Password> DRP </password>
- </DB-Info>
- </Config>
Class for reading XML files:
[Java]View Plain Copy
- Package com. util;
- Import java. Io. inputstream;
- Import java. util. hashmap;
- Import java. util. List;
- Import java. util. Map;
- Import org. dom4j. Document;
- Import org. dom4j. extends entexception;
- Import org. dom4j. element;
- Import org. dom4j. Io. saxreader;
- /**
- * Parse sys-config.xml files
- * @ Author Ronaldinho
- *
- */
- Public class xmlconfigreader {
- // Lazy
- Private Static xmlconfigreader instance = NULL;
- // Save JDBC-related information
- Private jdbcconfig = new jdbcconfig ();
- Private xmlconfigreader (){
- // Create a saxreader object
- Saxreader reader = new saxreader ();
- // Obtain the relative path of the file through the class loader of the current thread and read the buffered input stream
- Inputstream in = thread. currentthread (). getcontextclassloader (). getresourceasstream ("sys-config.xml ");
- Try {
- // Read XML files through streams
- Document Doc = reader. Read (in );
- // Read JDBC-related information
- Element drivernameelt = (element) Doc. SelectObject ("/config/DB-info/driver-name ");
- Element urlelt = (element) Doc. SelectObject ("/config/DB-info/url ");
- Element usernameelt = (element) Doc. SelectObject ("/config/DB-info/user-name ");
- Element passwordelt = (element) Doc. SelectObject ("/config/DB-info/password ");
- // Set JDBC Information
- Jdbcconfig. setdrivername (drivernameelt. getstringvalue ());
- Jdbcconfig. seturl (urlelt. getstringvalue ());
- Jdbcconfig. setusername (usernameelt. getstringvalue ());
- Jdbcconfig. setpassword (passwordelt. getstringvalue ());
- } Catch (incluentexception e ){
- E. printstacktrace ();
- }
- }
- Public static synchronized xmlconfigreader getinstance (){
- If (instance = NULL ){
- Instance = new xmlconfigreader ();
- }
- Return instance;
- }
- }
The above method generates an object in singleton mode. This object instantiates a saxreader and then loads the XML file into the stream. Then, use the read () method of saxreader to convert it into a document object. Then, the document object is used to get the node value of the XML file.
Today, we will briefly introduce how to use dom4j to read XML files. We will talk about other usage later. Don't worry.
PS: Thank you for your criticism!
Dom4j reads XML