java怎么将数据导出为xml文件
5个回答
展开全部
给你一个将数据库中表格生成xml文件的简单例子,希望对你有帮助
package book;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.*;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class SqlBean {
Connection con=null;
PreparedStatement stmt=null;
ResultSet rs=null;
public SqlBean()
{
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con=DriverManager.getConnection("","","");
stmt=con.prepareStatement("select * from book order by id");
}catch(Exception e)
{
e.printStackTrace();
}
}
public int JdomParser(){
this.query();
int m=1;
try{
Element root,book,bookname,author,price;
root=new Element("BookHome");
Document doc=new Document(root);
ResultSetMetaData rsmd=rs.getMetaData();
int colCount=rsmd.getColumnCount();
while(rs.next())
{
book=new Element("book");
bookname=new Element(rsmd.getColumnName(2));
author=new Element(rsmd.getColumnName(3));
price=new Element(rsmd.getColumnName(4));
bookname.setText(rs.getString(2));
author.setText(rs.getString(3));
price.setText(rs.getString(4));
book.addContent(bookname);
book.addContent(author);
book.addContent(price);
book.setAttribute(rsmd.getColumnName(1), rs.getString(1));
root.addContent(book);
}
Format format=Format.getCompactFormat();
format.setEncoding("gb2312");
format.setIndent(" ");
XMLOutputter XMLOut=new XMLOutputter(format);
XMLOut.output(doc, new FileOutputStream("D:/workspace/XML/book.xml"));
m=0;
}catch(Exception e){
e.printStackTrace();
}
finally
{
this.close();
}
return m;
}
public ResultSet query()
{
try{
rs=stmt.executeQuery();
}catch(Exception e){
e.printStackTrace();
}
return rs;
}
public void close()
{
try{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(con!=null)
con.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
package book;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.*;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class SqlBean {
Connection con=null;
PreparedStatement stmt=null;
ResultSet rs=null;
public SqlBean()
{
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con=DriverManager.getConnection("","","");
stmt=con.prepareStatement("select * from book order by id");
}catch(Exception e)
{
e.printStackTrace();
}
}
public int JdomParser(){
this.query();
int m=1;
try{
Element root,book,bookname,author,price;
root=new Element("BookHome");
Document doc=new Document(root);
ResultSetMetaData rsmd=rs.getMetaData();
int colCount=rsmd.getColumnCount();
while(rs.next())
{
book=new Element("book");
bookname=new Element(rsmd.getColumnName(2));
author=new Element(rsmd.getColumnName(3));
price=new Element(rsmd.getColumnName(4));
bookname.setText(rs.getString(2));
author.setText(rs.getString(3));
price.setText(rs.getString(4));
book.addContent(bookname);
book.addContent(author);
book.addContent(price);
book.setAttribute(rsmd.getColumnName(1), rs.getString(1));
root.addContent(book);
}
Format format=Format.getCompactFormat();
format.setEncoding("gb2312");
format.setIndent(" ");
XMLOutputter XMLOut=new XMLOutputter(format);
XMLOut.output(doc, new FileOutputStream("D:/workspace/XML/book.xml"));
m=0;
}catch(Exception e){
e.printStackTrace();
}
finally
{
this.close();
}
return m;
}
public ResultSet query()
{
try{
rs=stmt.executeQuery();
}catch(Exception e){
e.printStackTrace();
}
return rs;
}
public void close()
{
try{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(con!=null)
con.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
展开全部
思路:使用xml工具包(有很多不同的实现,比如dom4j、dom、jdom、sax)实现xml文档的生成然后保存即可。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
使用dom4j框架,里面有生成方式的文档说明的,具体代码请百度哦亲
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
dom4j~使用很简单的,网上找找
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
就和平常导出文件一样,只是后缀是xml,内容按照标签进行加入文件
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询