解析xml檔案

來源:互聯網
上載者:User

標籤:style   blog   color   java   os   io   檔案   資料   

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 = 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 = 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 ;}}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.