Brief introduction
DOM4J is an open source XML parsing package produced by dom4j.org. This sentence is too official, let's take a look at the official explanation. Such as:
DOM4J is an easy-to-use, open-source library for parsing languages such as Xml,xpath and XSLT. It is applied to the Java platform, uses the Java Collection framework, and fully supports programming standards such as Dom,sax and JAXP.
Characteristics
dom4j is a very, very good Java XML API that features excellent performance, power, and extreme ease of use, as well as an open source software. Now you can see that more and more Java software is using dom4j to read and write XML, such as Hibernate, including Sun's own JAXM dom4j.
Download
since DOM4J has the advantage of the above n many, we must uncover the dom4j's mystery. with dom4j development, you need to download the dom4j appropriate jar file.
1. Website Download:: HTTP://WWW.DOM4J.ORG/DOM4J-1.6.1/
2.DOM4J is an open source project on SourceForge.net, so you can download its latest version to http://sourceforge.net/projects/dom4j
Open dom4j-1.6.1 Unzip file, we can see the folder with docs Help, also have dom4j parse XML file Dom4j-1.6.1.jar file. We just need to build the Dom4j-1.6.1.jar file into our development project and we can use dom4j to develop it.
Instance
Below we use DOM4J to read the system configuration file to enable the ability to connect to the Oracle database. Before you begin, build the jars we need into our projects, such as:
1. Copy the Oracle JDBC driver to Web-inf/lib
2. Copy dom4j related jar to Web-inf/lib
(1) Dom4j-1.6.1.jar
(2) Jaxen-1.1-beta-6.jar ( support for XPath-related jar packages )
Connection Database class: Dbuitl.java
Package Util;import Java.sql.connection;import Java.sql.drivermanager;import Java.sql.sqlexception;public class Dbutil {/** * obtained Connection * @return */public static Connection getconnection () {Connection Conn=null;try {jdbcconfig Jdbcconfig = Xmlconfigreader.getinstance (). Getjdbcconfig ();//Get the Oracle-driven path Class.forName (jdbcconfig.getdrivername ());//String URL = "Jdbc:oracle:thin: @MyDbComputerNameOrIP: 1521:ORCL";//database connection, Oracle is connected to Oracle database; thin:@ Mydbcomputernameorip represents the IP address where the database resides (which can be reserved thin:),//1521 represents the port number that the database is connected to, and ORCL represents the database name conn = Drivermanager.getconnection ( Jdbcconfig.geturl (), Jdbcconfig.getusername (), Jdbcconfig.getpassword ());} catch (ClassNotFoundException e) {e.printstacktrace ();} catch (SQLException e) {e.printstacktrace ();} Return conn;}}
Database configuration file: Sys-config.xml
<?xml version= "1.0" encoding= "UTF-8"?><config><db-info><driver-name> Oracle.jdbc.driver.oracledriver</driver-name><url>jdbc:oracle:thin: @localhost: 1521:oracle</url ><user-name>drp1</user-name><password>drp1</password></db-info></config >
configuration information for JDBC: Jdbcconfig.java
Package util;/** * JDBC configuration information * @author Liang * */public class Jdbcconfig {private string Drivername;private string Url;priv Ate string Username;private string Password;public string Getdrivername () {return drivername;} public void Setdrivername (String drivername) {this.drivername = drivername;} Public String GetUrl () {return URL;} public void SetUrl (String url) {this.url = URL;} Public String GetUserName () {return userName;} public void Setusername (String userName) {this.username = UserName;} Public String GetPassword () {return password;} public void SetPassword (String password) {this.password = password;}}
dom4j Read Xml:XmlConfigReader.java
Package Util;import Java.io.inputstream;import Org.dom4j.document;import org.dom4j.documentexception;import Org.dom4j.element;import org.dom4j.io.saxreader;/** * Using a single case resolution Sys-config.xml file * @author Liang * */public class Xmlconfigreader {//Singleton lazy (lazy load lazy)//set to NULL, use in new, without newprivate static xmlconfigreader instance = null;// Define JDBCCONFIG member variables, save JDBC-related configuration information private jdbcconfig jdbcconfig = new Jdbcconfig (); Private Xmlconfigreader () {///Create Saxreader object Saxreader reader = new Saxreader ();//through the current thread's ClassLoader, get the relative path of the file, Reads the XML file into the input stream inputstream in = Thread.CurrentThread (). Getcontextclassloader (). getResourceAsStream ("Sys-config.xml") try {//Read the XML file via the Read method, convert to Document object Document DOC = Reader.read (in);//Get the Node object, get JDBC-related configuration information element drivernameelt = (El ement) 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 configuration 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;} /** * Returns the relevant configuration of JDBC * @return */public jdbcconfig getjdbcconfig () {return jdbcconfig;}}
parsing: read-write XML documents are mainly dependent on the Org.dom4j.io package, which provides two different ways of Domreader and Saxreader, and the same invocation method. This is the benefit of relying on interfaces.
The Read method of reader is overloaded and can be read from various sources such as InputStream, File, URL, and the resulting Document object represents the entire XML.
DOM4J Four steps to parse XML
1. Create Saxreader objects in singleton mode
2. Read the XML file into the input stream
3. The Read method reads the XML file and transforms it into a Document object
4. The node value of the XML file is taken by the Document object
Summarize
As the saying goes: stand on the shoulders of giants. dom4j is a giant, can clearly understand the dom4j, and will use on OK.
Support Source Download: dom4j parse XML, connect Oracle Database