首页 > 代码库 > 解析xml文件

解析xml文件

package com.cn.javaSE.dom4j;import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Date;import java.util.Iterator;import java.util.List;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.DocumentHelper;import org.dom4j.Element;import org.dom4j.Node;import org.dom4j.io.OutputFormat;import org.dom4j.io.SAXReader;import org.dom4j.io.XMLWriter;public class WriteXml extends BaseDao {/*** @param args* @throws SQLException* @throws ClassNotFoundException* @throws IOException*/private Connection conn;public static void main(String[] args) throws SQLException,    ClassNotFoundException {   // TODO Auto-generated method stub   //new WriteXml().WriteXmlFile("d://person.xml");   new WriteXml().ReadXmlFile("d://person.xml");}/*** @return 定义一个方法,用于从数据库中读取数据,然后把数据集存到xml文件中;* @throws ClassNotFoundException* @throws SQLException*/public int WriteXmlFile(String file) throws ClassNotFoundException,    SQLException {   conn = this.getConn();   Statement st = null;   try {    st = conn.createStatement();   } catch (SQLException e1) {    // TODO Auto-generated catch block    e1.printStackTrace();   }   String sql = "select * from person";   ResultSet rs = st.executeQuery(sql);   Document document = DocumentHelper.createDocument();// 格式文件的输出;   document.setXMLEncoding("gb2312");   Date date = new Date();   String s = date.toLocaleString();   int value = http://www.mamicode.com/0; // 判断文件是否写入成功的标记;   // 为xml添加注释;   document.addComment("从数据库meemei中的表person读取数据放到person.xml文件中!修改日期:" + s);   Element root = document.addElement("meemei");   OutputFormat format = OutputFormat.createPrettyPrint();   format.setEncoding("gb2312");   while (rs.next()) {    // 从数据库中读取数据    Integer id = rs.getInt(1);    String user = rs.getString(2);    Float salary = rs.getFloat(3);    Integer age = rs.getInt(4);    String dept1 = rs.getString(5);    System.out.println(id + user);    Element element = root.addElement("person");    Element ID = element.addElement("id");    Element pname = element.addElement("pname");    Element psalary = element.addElement("psalary");    Element page = element.addElement("page");    Element dept = element.addElement("dept");    // 把从数据库中读取的数据放到xml中;    ID.addText(String.valueOf(id));    pname.addText(user);    psalary.addText(String.valueOf(salary));    page.addText(String.valueOf(age));    dept.addText(dept1);    XMLWriter writer;    // 构造文件输出流;    try {     writer = new XMLWriter(new FileWriter(new File(file)), format);     writer.write(document);     writer.close();     value = 1;// 执行成功,返回1 ;    } catch (IOException e) {     // TODO Auto-generated catch block     e.printStackTrace();    }   }   return value;}/*** 从一个xml文件中读取数据,然后存到数据库中;** @return*/public int ReadXmlFile(String file) {   conn = this.getConn();   SAXReader reader = new SAXReader();   int value = http://www.mamicode.com/0 ;   try {    Document doc = reader.read(new File(file));    Element element = doc.getRootElement();    System.out.println("根节点:"+element.getName());//获得根节点    List<Element> list = element.elements() ;    Iterator<Element> it = list.iterator() ;       String database = element.getName();    System.out.println("数据库名:"+database);    String table = "" ;    Integer id = 0;    String pname = "" ;    Float psalary = 0.0f ;    Integer page = 0 ;    String dept = "" ;       while ( it.hasNext() ) {     Element e = it.next() ;     System.out.println("---------------------------------------");     System.out.println("表名:"+e.getName());     table = e.getName () ;     Iterator itor = e.elementIterator() ;     while ( itor.hasNext() ) {     //-------------id------------     Node idNode = (Node) itor.next();     String idName = idNode.getName();     String idValue = idNode.getText();     id = Integer.valueOf(idValue);     System.out.println("节点:"+idName+"---->>值:"+idValue);     //------------pname-------------     Node nameNode = (Node) itor.next();     String NameName = nameNode.getName();     String NameValue = nameNode.getText();     pname = NameValue ;     System.out.println("节点:"+NameName+"---->>值:"+NameValue);     //-------------psalary -------------     Node salaryNode =(Node) itor.next();     String salaryName = salaryNode.getName();     String salaryValue = salaryNode.getText();     psalary = Float.valueOf(salaryValue) ;     System.out.println("节点:"+salaryName+"---->>值:"+salaryValue);     //--------------page--------------     Node ageNode = (Node) itor.next();     String ageName = ageNode.getName();     String ageValue = ageNode.getText();     page = Integer.valueOf(ageValue);     System.out.println("节点:"+ageName+"---->>值:"+ageValue);     //--------------dept--------------     Node deptNode =(Node) itor.next();     String deptName = deptNode.getName();     String deptValue = deptNode.getText();     dept = deptValue ;     System.out.println("节点:"+deptName+"---->>值:"+deptValue);     String sql = " insert into "+table+ " values (?,?,?,?,?) ";     try {      PreparedStatement ps = conn.prepareStatement(sql);      ps.setInt(1, id);      ps.setString(2, pname);      ps.setFloat(3, psalary);      ps.setInt(4, page);      ps.setString(5, dept);      ps.executeUpdate();               } catch (SQLException e1) {      // TODO Auto-generated catch block      e1.printStackTrace();     }     System.out.println(sql);     }        }         } catch (DocumentException e) {    // TODO Auto-generated catch block    e.printStackTrace();   }   return value ;}}