dom4j写出xml文件

来源:互联网 发布:配衣服的软件 编辑:程序博客网 时间:2024/06/06 05:45

dom4j写出xml文件简单实例:

 

import java.sql.*; //Connection DriverManager Statementimport java.io.*;//File FileInputStreamimport org.dom4j.*;//Document Element Attributeimport org.dom4j.io.*;//SAXReader//将Emp表导出/*<emps><emp><empno></empno><ename></ename></emp></emps>//DOM4J创建文件1.内存中创建一个Document对象 空的2.添加各种内容3.写出到文件*/public class Exec{public static void main(String args[])throws Exception{export();}public static void export()throws Exception{Document doc = DocumentHelper.createDocument();//root ElementElement root = doc.addElement("emps");Connection con = getConnection();Statement sta = con.createStatement();String sql="select * from emp";ResultSet rs = sta.executeQuery(sql);while(rs.next()){Element first = root.addElement("emp");Element empno = first.addElement("empno");empno.setText(rs.getInt("empno")+"");Element ename = first.addElement("ename");ename.setText(rs.getString("ename"));}//关闭资源rs.close();sta.close();con.close();//写出XMLWriter writer = new XMLWriter(new FileOutputStream("emp.xml"),OutputFormat.createPrettyPrint());writer.write(doc);writer.close();}//获得连接的方法private static Connection getConnection()throws Exception{Class.forName("oracle.jdbc.driver.OracleDriver");Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","tiger");return con;}}


 

0 0
原创粉丝点击