生活随笔
收集整理的這篇文章主要介紹了
使用dom4j解析XML文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
為什么80%的碼農都做不了架構師?>>> ??
dom4j是一個java 的XML API,用來讀寫XML文件。
使用dom4j解析XML文件,首先需要在官網下載dom4j-1.6.1.zip。
URL:http://sourceforge.net/projects/dom4j/files/latest/download?source=files。
創建的Java 工程目錄結構:
我們以解析數據庫連接和配置文件c3p0.xml為例。
c3p0.xml
<?xml?version="1.0"?encoding="UTF-8"?>
<c3p0-config><init><name>Oracle</name><driver>oracle.jdbc.driver.OracleDriver</driver><url>jdbc:oracle:thin:@192.168.1.29:1521:orcl</url><user>orcl</user><password>orcl</password><initPoolSize>5</initPoolSize><minPoolSize>2</minPoolSize><maxPoolSize>20</maxPoolSize><maxStatements>20</maxStatements><maxIdleTime>60</maxIdleTime><acquireIncrement>5</acquireIncrement><checkoutTimeout>3000</checkoutTimeout></init>
</c3p0-config>
C3p0.java
package?com.xml.parse.file;public?class?C3p0?{private?String?name;private?String?driver;private?String?url;private?String?user;private?String?password;private?String?initPoolSize;private?String?minPoolSize;private?String?maxPoolSize;private?String?maxStatements;private?String?maxIdleTime;private?String?acquireIncrement;private?String?checkoutTimeout;public?C3p0()?{}public?C3p0(String?name,?String?driver,?String?url,?String?user,String?password,?String?initPoolSize,?String?minPoolSize,String?maxPoolSize,?String?maxStatements,?String?maxIdleTime,String?acquireIncrement,?String?checkoutTimeout)?{this.name?=?name;this.driver?=?driver;this.url?=?url;this.user?=?user;this.password?=?password;this.initPoolSize?=?initPoolSize;this.minPoolSize?=?minPoolSize;this.maxPoolSize?=?maxPoolSize;this.maxStatements?=?maxStatements;this.maxIdleTime?=?maxIdleTime;this.acquireIncrement?=?acquireIncrement;this.checkoutTimeout?=?checkoutTimeout;}public?String?getName()?{return?name;}public?void?setName(String?name)?{this.name?=?name;}public?String?getDriver()?{return?driver;}public?void?setDriver(String?driver)?{this.driver?=?driver;}public?String?getUrl()?{return?url;}public?void?setUrl(String?url)?{this.url?=?url;}public?String?getUser()?{return?user;}public?void?setUser(String?user)?{this.user?=?user;}public?String?getPassword()?{return?password;}public?void?setPassword(String?password)?{this.password?=?password;}public?String?getInitPoolSize()?{return?initPoolSize;}public?void?setInitPoolSize(String?initPoolSize)?{this.initPoolSize?=?initPoolSize;}public?String?getMinPoolSize()?{return?minPoolSize;}public?void?setMinPoolSize(String?minPoolSize)?{this.minPoolSize?=?minPoolSize;}public?String?getMaxPoolSize()?{return?maxPoolSize;}public?void?setMaxPoolSize(String?maxPoolSize)?{this.maxPoolSize?=?maxPoolSize;}public?String?getMaxStatements()?{return?maxStatements;}public?void?setMaxStatements(String?maxStatements)?{this.maxStatements?=?maxStatements;}public?String?getMaxIdleTime()?{return?maxIdleTime;}public?void?setMaxIdleTime(String?maxIdleTime)?{this.maxIdleTime?=?maxIdleTime;}public?String?getAcquireIncrement()?{return?acquireIncrement;}public?void?setAcquireIncrement(String?acquireIncrement)?{this.acquireIncrement?=?acquireIncrement;}public?String?getCheckoutTimeout()?{return?checkoutTimeout;}public?void?setCheckoutTimeout(String?checkoutTimeout)?{this.checkoutTimeout?=?checkoutTimeout;}}
ParseXML.java:
package?com.xml.parse.file;import?java.io.InputStream;import?org.dom4j.Document;
import?org.dom4j.DocumentException;
import?org.dom4j.Element;
import?org.dom4j.io.SAXReader;public?class?ParseXML?{private?static?ParseXML?instance?=?null;private?C3p0?c3p0?=?new?C3p0();public?ParseXML(){SAXReader??reader?=?new?SAXReader();InputStream?in?=?Thread.currentThread().getContextClassLoader().getResourceAsStream("config/c3p0.xml");try?{Document?document?=?reader.read(in);Element?root?=?document.getRootElement();Element?e?=?root.element("init");Element?name?=?(Element)e.element("name");Element?driver?=?(Element)e.element("driver");Element?url?=?(Element)e.element("url");Element?user?=?(Element)e.element("user");Element?password?=?(Element)e.element("password");Element?initPoolSize?=?(Element)e.element("initPoolSize");Element?minPoolSize?=?(Element)e.element("minPoolSize");Element?maxPoolSize?=?(Element)e.element("maxPoolSize");Element?maxStatements?=?(Element)e.element("maxStatements");Element?maxIdleTime?=?(Element)e.element("maxIdleTime");Element?acquireIncrement?=?(Element)e.element("acquireIncrement");Element?checkoutTimeout?=?(Element)e.element("checkoutTimeout");c3p0.setName(name.getStringValue());c3p0.setDriver(driver.getStringValue());c3p0.setUrl(url.getStringValue());c3p0.setUser(user.getStringValue());c3p0.setPassword(password.getStringValue());c3p0.setInitPoolSize(initPoolSize.getStringValue());c3p0.setMinPoolSize(minPoolSize.getStringValue());c3p0.setMaxPoolSize(maxPoolSize.getStringValue());c3p0.setMaxStatements(maxStatements.getStringValue());c3p0.setMaxIdleTime(maxIdleTime.getStringValue());c3p0.setAcquireIncrement(acquireIncrement.getStringValue());c3p0.setCheckoutTimeout(checkoutTimeout.getStringValue());}?catch?(DocumentException?e)?{e.printStackTrace();}}public?static?synchronized?ParseXML?getInstance()?{if(instance?==?null){instance?=?new?ParseXML();}return?instance;}public?C3p0?getC3p0()?{return?c3p0;}}
XMLTest.java:
package?com.xml.parse.file;public?class?XMLTest?{public?static?void?main(String?args[]){ParseXML?xml?=?ParseXML.getInstance();System.out.println("---name---");System.out.println(xml.getC3p0().getName());System.out.println("---driver---");System.out.println(xml.getC3p0().getDriver());System.out.println("---url---");System.out.println(xml.getC3p0().getUrl());System.out.println("---user---");System.out.println(xml.getC3p0().getUser());System.out.println("---password---");System.out.println(xml.getC3p0().getPassword());System.out.println("---initPoolSize---");System.out.println(xml.getC3p0().getInitPoolSize());System.out.println("---minPoolSize---");System.out.println(xml.getC3p0().getMinPoolSize());System.out.println("---maxPoolSize---");System.out.println(xml.getC3p0().getMaxPoolSize());System.out.println("---maxStatements---");System.out.println(xml.getC3p0().getMaxStatements());System.out.println("---maxIdleTime---");System.out.println(xml.getC3p0().getMaxIdleTime());System.out.println("---acquireIncrement---");System.out.println(xml.getC3p0().getAcquireIncrement());System.out.println("---checkoutTimeout---");System.out.println(xml.getC3p0().getCheckoutTimeout());}
}
運行XMLTest.java中的main方法。結果如下:
好,到此使用dom4j解析XML文件完畢,請大家指正!
轉載于:https://my.oschina.net/douxn/blog/425060
總結
以上是生活随笔為你收集整理的使用dom4j解析XML文件的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。