How to read xml from dom4j in java
I have learned about XML in BS, but I have always understood XML as "not important" and "Can I do anything ". But in the absence of xml, programmers must design their own transmission interfaces to transfer data between processes, which involves many annoying data formats, this prevents programmers from focusing on specific business solutions. Now, xml and various languages support the xml package, freeing programmers.
After learning about XML, let's talk about dom4j. Dom4j is a Java xml api, similar to jdom, used to read and write XML files. Dom4j is a very good JavaXML API with excellent performance, powerful functionality, and extremely easy to use features. It is also an open source software that can be found on SourceForge. You can also find an article on IBM developerWorks to evaluate the performance, functionality, and usability of mainstream Java XML APIs. Therefore, you can know that dom4j is excellent in any aspect. Now we can see that 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. Hibernate also uses it to read and write configuration files.
Let's take a look at the instances in DRP.
XML file:
oracle.jdbc.driver.OracleDriver
jdbc:oracle:thin:@127.0.0.1:1521:ORCL
sys
sys
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.
In the code, the reader's read method is overloaded and can be read through different parameters such as InputStream, File, Url, and so on. An object is generated in singleton mode, and this object is instantiated as a SAXReader, then load 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.
Java class for reading XML:
Package conn. lishehe. drp. util; import java. io. inputStream; // reference the corresponding package import org. dom4j. document; import org. dom4j. extends entexception; import org. dom4j. element; import org. dom4j. io. SAXReader; public class XmlConfigReader {// lazy private static XmlConfigReader instance = null; // private JdbcConfig jdbcConfig = new JdbcConfig () for saving jdbc-related configuration information; private XmlConfigReader () {// create a SAXReader object SAXReader reader = new SAXRea Der (); // obtain the relative path of the file through the class loader of the current Thread, and read the buffer input stream InputStream in = Thread. currentThread (). getContextClassLoader (). getResourceAsStream ("sys-config.xml"); try {// get jdbc-related configuration information // read the xml file Document doc = reader through the stream. read (in); // read jdbc-related information // Element: used to describe elements in the XML document. 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"); // obtain jdbc-related configuration jdbcConfig. setDriverName (driverNameElt. getStringValue (); jdbcConfig. setUrl (urlElt. getStringValue (); jdbcConfig. setUserName (userNameElt. getStringValue (); jdbcConfig. setPassword (passwordElt. getStringValue ();} catch (incluentexception e) {// TODO Auto-generated catch blocke. printStackTrace () ;}} public static synchronized XmlConfigReader getInstance () {if (instance = null) {instance = new XmlConfigReader ();} return instance ;} /*** return jdbc configuration * @ return */public JdbcConfig getJdbcConfig () {return jdbcConfig;} public static void main (String [] args) {JdbcConfig jdbcConfig = XmlConfigReader. getInstance (). getJdbcConfig (); System. out. println (jdbcConfig );}}
Define object class: JdbcConfig. java
Package conn. lishehe. drp. util;/*** Jdbc * @ author Xu zhipeng **/public class JdbcConfig {private String driverName; 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;} private String url; private String userName; private String password; @ Overridepublic String toString () {return this. getClass (). getCanonicalName () + "{driverName:" + driverName + ", url:" + url + ", userName:" + userName + "}";}}
Data Processing Layer: DbUtil. java
Package conn. lishehe. drp. util; import java. SQL. connection; import java. SQL. driverManager; /*** encapsulate Common Database Operations ** @ author Xu zhipeng */public class DbUtil {/*** get Connection * @ return * @ throws ClassNotFoundException */public static Connection getConnection () throws ClassNotFoundException {Connection conn = null; try {JdbcConfig jdbcConfig = XmlConfigReader. getInstance (). getJdbcConfig (); Class. forName (jdbcConfig. getDriverName (); conn = DriverManager. getConnection (jdbcConfig. getUrl (), jdbcConfig. getUserName (), jdbcConfig. getPassword ();} catch (Exception e) {// TODO Auto-generated catch blocke. printStackTrace ();} return conn;} public static void main (String [] args) {try {// test to obtain the information of the corresponding configuration file and generate System. out. println (DbUtil. getConnection ();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}I still need to remember and summarize the above Code. In fact, it is not difficult to understand it as much as I do. Come on !!!