dom4j is a Java XML API, similar to jdom, used to read and write XML files. dom4j is a very, very excellent Java XML API, with excellent performance, powerful functions and extremely easy to use. It is also an open source software. Now more and more Java software is using dom4j to read and write XML. It is particularly worth mentioning that even Sun's JAXM is using dom4j. This is a must-use jar package.
The above said that dom4j is so excellent and easy to use. So from today on, I will share with you some usages of dom4j.
The main interfaces of dom4j are defined in the org.dom4j package:
To understand this set of interfaces, the key is to understand the inheritance relationship of the interface:
Reading and writing XML documents mainly rely on the org.dom4j.io package, which provides two different ways of DOMReader and SAXReader, but the call method is the same. This is the benefit of relying on interfaces.
// Read XML from the file, enter the file name, and return the XML document public Document read(String fileName) throws MalformedURLException, DocumentException { SAXReader reader = new SAXReader(); Document document = reader.read(new File(fileName)); return document; } The read method of reader is overloaded and can be read through different parameters such as InputStream, File, Url, etc. The resulting Document object lists the entire XML.
According to my own experience, the character encoding read is converted according to the encoding defined in the XML file header. If you encounter garbled code problems, be careful to keep the encoding names in various places consistent.
The following example is the SAXReader class reads an xml file through InputStream:
The xml file to be read:
<?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>
Classes that read xml files:
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.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; /** * parse sys-config.xml file* @author Ronaldinho * */ public class XmlConfigReader { // Lazy private static XmlConfigReader instance = null; //Save jdbc related information private JdbcConfig jdbcConfig = new JdbcConfig(); private XmlConfigReader() { //Create a SAXReader object SAXReader reader=new SAXReader(); //Use the class loader of the current thread to obtain the relative path of the file and read the buffered input stream InputStream in=Thread.currentThread().getContextClassLoader().getResourceAsStream("sys-config.xml"); try { //Read the xml file Document through stream doc=reader.read(in); //Read jdbc related informationElement 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 related information jdbcConfig.setDrivername(driverNameElt.getStringValue()); jdbcConfig.setUrl(urlElt.getStringValue()); jdbcConfig.setUsername(userNameElt.getStringValue()); jdbcConfig.setPassword(passwordElt.getStringValue()); } catch (DocumentException e) { e.printStackTrace(); } } public static synchronized XmlConfigReader getInstance() { if (instance==null) { instance = new XmlConfigReader(); } return instance; } }The above method is to generate an object through singleton pattern, which instantiates a SAXReader, and then loads the xml file into the stream. Then convert it into a document object through the read() method of SAXReader. Then use this document object to get the value of the node in the xml file.
Today I will briefly introduce the use of dom4j to read xml files. I will talk to you about other usages later. Don't worry, everyone.
PS: Everyone is welcome to criticize and correct me!
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.