第一:首先需要导入所需的jar文件:
如图所示:
下面代码是完整的可运行的解析xml文件的代码:package Dom4J;
import java.io.FileInputStream;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class TestDOM4JParseXML {
/**
* @Title: main
* @Description: TODO
* @author zhanghao
* @throws
*/
public static void main(String[] args) {
SAXReader reader = new SAXReader();
FileInputStream fis = null;
try{
fis = new FileInputStream("stu.xml");
//加载xml文档
Document doc = reader.read(fis);
//获取根元素
Element rootElt = doc.getRootElement();
System.out.println(rootElt.getName());
List elements = rootElt.elements();
System.out.println(elements.size());
for(int i=0;i<elements.size();i++){
Element elt = (Element)elements.get(i);
System.out.println(elt.getName());
List eltList = elt.attributes();
for(int j=0;j<eltList.size();j++){
Attribute attr = (Attribute)eltList.get(j);
System.out.println("\t属性:"+attr.getName()+"="+attr.getValue());
}
List subEletList = elt.elements();
for(int j=0;j<subEletList.size();j++){
Element subElt = (Element)subEletList.get(j);
System.out.println("\t子元素:"+subElt.getName()+"="+subElt.getText());
}
}
}catch (Exception e) {
e.printStackTrace();
}finally{
try{
if(fis !=null)
fis.close();
}catch (Exception e) {
e.printStackTrace();
}finally{
}
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<学生们>
<学生 id="001">
<姓名>张三</姓名>
<学号>001</学号>
</学生>
<学生>
<姓名>李四</姓名>
<学号>002</学号>
</学生>
<学生>
<姓名>王五</姓名>
<学号>003</学号>
</学生>
<学生 姓名="李刘" 学号="004"></学生>
</学生们>
package Dom4J;
import java.io.FileWriter;
import java.io.IOException;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;
public class TestDOM4JCreateXML {
/**
* @Title: main
* @Description: TODO
* @author zhanghao
* @throws
*/
public static void main(String[] args) {
//创建Document实例
Document doc = DocumentHelper.createDocument();
Element rootElt = doc.addElement("select");
rootElt.addAttribute("name","city");
Element city1Elt1 = rootElt.addElement("option");
city1Elt1.addAttribute("value","1");
city1Elt1.addText("北京市");
Element city1Elt2 = rootElt.addElement("option");
city1Elt2.addAttribute("value","2");
city1Elt2.addText("天津市");
Element city1Elt3 = rootElt.addElement("option");
city1Elt3.addAttribute("value","3");
city1Elt3.addText("广西市");
Element city1Elt4 = rootElt.addElement("option");
city1Elt4.addAttribute("value","4");
city1Elt4.addText("河南市");
doc.setXMLEncoding("UTF-8");
String xmlStr = doc.asXML();
System.out.println(xmlStr);
XMLWriter writer = null;
FileWriter fw = null;
try{
fw = new FileWriter("d:\\city.xml");
//doc.write(fw);
writer = new XMLWriter(fw);
writer.write(doc);
System.out.println("写入成功");
}catch(IOException e){
e.printStackTrace();
}finally{
try {
if(fw != null){
fw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
主要目录可以自己定义:
DBUtil.java
package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.sqlException;
public class DBUtil {
private static DBUtil instance;
private jdbcInfo jdbcInfo;
private DBUtil(){
this.jdbcInfo = SystemUtil.getInstance().getJdbcInfo();
try {
Class.forName(this.jdbcInfo.getDriverClassName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static DBUtil getInstance(){
if(instance == null){
instance = new DBUtil();
}
return instance;
}
public Connection getConnection(){
Connection conn = null;
try {
conn = DriverManager.getConnection(this.jdbcInfo.getUrl(),
this.jdbcInfo.getUsername(),
this.jdbcInfo.getPassword());
} catch (sqlException e) {
e.printStackTrace();
}
return conn;
}
public void free(Connection conn,java.sql.Statement stmt,ResultSet rs){
try {
if(rs != null)
rs.close();
if(stmt != null)
stmt.close();
if(conn != null)
conn.close();
} catch (sqlException e) {
e.printStackTrace();
}
}
}
JdbcInfo.java
package util;
public class jdbcInfo {
private String driverClassName;
private String url;
private String username;
private String password;
public String getDriverClassName() {
return driverClassName;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
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;
}
}
SystemUtil.java
package util;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
public class SystemUtil {
private static SystemUtil instance;
private jdbcInfo jdbcInfo;
private Element rootElt;
public static final String CFG_FILE_NAME = "system.cfg.xml";
private SystemUtil(){
SAXReader reader = new SAXReader();
try {
Document doc = reader.read(Thread.currentThread().getContextClassLoader().getResource(CFG_FILE_NAME));
System.out.println(doc);
rootElt = doc.getRootElement();
this.initJdbcInfo();
} catch (DocumentException e) {
e.printStackTrace();
}
}
private void initJdbcInfo(){
this.jdbcInfo = new jdbcInfo();
Node driverClassNode = rootElt.selectSingleNode("/system-configuration/jdbc-info/property[@name='driver_class']");
String driverClassName = driverClassNode.getText();
this.jdbcInfo.setDriverClassName(driverClassName);
Node urlNode = rootElt.selectSingleNode("/system-configuration/jdbc-info/property[@name='url']");
String url = urlNode.getText();
this.jdbcInfo.setUrl(url);
Node usernameNode = rootElt.selectSingleNode("/system-configuration/jdbc-info/property[@name='username']");
String username = usernameNode.getText();
this.jdbcInfo.setUsername(username);
Node passwordNode = rootElt.selectSingleNode("/system-configuration/jdbc-info/property[@name='password']");
String password = passwordNode.getText();
this.jdbcInfo.setPassword(password);
System.out.println(driverClassName);
}
public jdbcInfo getJdbcInfo(){
return this.jdbcInfo;
}
public static SystemUtil getInstance(){
if(instance == null){
instance = new SystemUtil();
}
return instance;
}
public static void main(String[] args){
new SystemUtil();
}
@Override
public String toString() {
return super.toString();
}
}
System.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<system-configuration>
<jdbc-info>
<property name="driver_class">com.MysqL.jdbc.Driver</property>
<property name="url">jdbc:MysqL://192.168.1.210:3306/my_db</property>
<property name="username">simope</property>
<property name="password">simope#CTCluster#2015</property>
</jdbc-info>
</system-configuration>
testClient.java
package tset; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.sqlException; import util.DBUtil; public class TestClient { /** * @Title: main * @Description: TODO * @author zhanghao * @date 2016-1-16 上午10:45:32 * @throws */ public static void main(String[] args) { DBUtil util = DBUtil.getInstance(); Connection conn =null ; PreparedStatement stmt = null; ResultSet rs = null; conn = util.getConnection(); String sql = "select * from sys_user"; try { stmt = conn.prepareStatement(sql); rs = stmt.executeQuery(); while(rs.next()){ int id = rs.getInt("userID"); String name = rs.getString("password"); System.out.println("id:"+id+"\tName:"+name); } } catch (sqlException e) { e.printStackTrace(); }finally{ util.free(conn,stmt,rs); } } }