??????? 總是在網(wǎng)絡(luò)上copy別人的源代碼,今天我也貼出自己今天寫的源碼,相信這個(gè)程序會(huì)對(duì)大家在平時(shí)的工作中需要頻繁從數(shù)據(jù)庫(kù)中提取數(shù)據(jù)轉(zhuǎn)化成xml文件會(huì)有幫助。
????最近公司項(xiàng)目中有一件事就是從數(shù)據(jù)庫(kù)表中讀出數(shù)據(jù),然后轉(zhuǎn)換成xml文件供客戶端下載,由于數(shù)據(jù)庫(kù)中表太多,不可能為單獨(dú)的每個(gè)表都寫一個(gè)轉(zhuǎn)換程序。于是,經(jīng)過分析,寫了一個(gè)通用的用ResultSet對(duì)象轉(zhuǎn)換成xml文件的程序。這樣,只需把查詢結(jié)果集(ResultSet對(duì)象)和要生成的xml文件的路徑傳入,然后自己指派屬性名稱、元素名稱并與對(duì)應(yīng)的查詢結(jié)果集中的字段相對(duì)應(yīng),最后調(diào)用designOver()函數(shù),就可以生成所期望的xml文件了。最后,給出了一個(gè)示例程序。
????我是用dom解析的,用純java語(yǔ)言編寫,程序包括5個(gè)類:CreateXmlFile.java、Disposal.java、SaveAttrName.java、SaveEleName.java、WriteXmlFile.java
真正與用戶交互的只有CreateXmlFile.java,如果不想了解程序邏輯,可不用管其他類。代碼講解及示例如下:
?
文件?CreateXmlFile.java?內(nèi)容:
package?currencycreatexml;

import?java.sql.*;


public?class?CreateXmlFile


{
?private?ResultSet?rs;?????//從下面的程序可以看出,此字段可省略,懶得改了,呵呵
?private?String?url;???????//從下面的程序可以看出,此字段可省略,懶得改了,呵呵
?private?Disposal?disposal;?//自定義的用來收集和處理數(shù)據(jù)的類
?private?String?root;???????//xml文件的根元素名稱
?private?String?rootchild;??//根結(jié)點(diǎn)的子結(jié)點(diǎn)的元素名稱

?/**?*//**
??*?@param?rs?:?創(chuàng)建xml文件所需的查詢結(jié)果集
??*?@param?url?:?指定xml文件的生成路徑(包括xml文件的文件名)
??*/
?public?CreateXmlFile(ResultSet?rs,String?url)

?
{
??this.rs=rs;
??this.url=url;
??disposal=new?Disposal();
??disposal.setResultSet(this.rs,this.url);
?}
?//設(shè)定xml文件的根元素名稱
?public?void?setRootElementName(String?root,String?rootchild)

?
{
??this.root=root;
??this.rootchild=rootchild;
??disposal.setRootName(this.root,this.rootchild);
?}
?//設(shè)置屬性的名字和索引位置,位置從1開始

?/**?*//**
??*?@param?namestring?指定屬性的名稱
??*?@param?index?指定此屬性的值在查詢結(jié)果集中第幾個(gè)字段(字段從1開始索引)
??*/
?public?void?setAttributeName(String?namestring,int?index)

?
{
??disposal.collectData(namestring,index,"attribute");
?}
?//設(shè)置元素的名字和索引位置,位置從1開始

?/**?*//**
??*?@param?namestring?指定元素的名稱
??*?@param?index?指定此元素的值在查詢結(jié)果集中的第幾個(gè)字段(字段從1開始索引)
??*/
?public?void?setElementName(String?namestring,int?index)

?
{
??disposal.collectData(namestring,index,"element");
?}

?/**?*//**
?*?調(diào)用此方法則立即開始創(chuàng)建xml文件,在屬性與元素都已指派完畢后調(diào)用此方法
?*/
?public?void?designOver()

?
{
??disposal.startWrite();
?}
}


文件?Disposal.java?內(nèi)容:
package?currencycreatexml;

import?java.util.*;
import?java.sql.*;

class?Disposal


{
?private?ResultSet?rs;
?private?String?url;
?private?ArrayList?attrilist=new?ArrayList();??//用來存儲(chǔ)屬性名和字段索引的集合類
?private?ArrayList?elelist=new?ArrayList();????//用來存儲(chǔ)元素名和字段索引的集合類
?private?String?root;
?private?String?rootchild;
?public?void?setResultSet(ResultSet?rs,String?url)

?
{
??this.rs=rs;
??this.url=url;
?}
?public?void?setRootName(String?root,String?rootchild)

?
{
??this.root=root;
??this.rootchild=rootchild;
?}
?@SuppressWarnings("unchecked")
?public?void?collectData(String?namestring,int?index,String?type?)

?
{
??if(type.equals("attribute"))
???attrilist.add(new?SaveAttrName(namestring,index));
??else
???elelist.add(new?SaveEleName(namestring,index));
???//System.out.println("else");
??//System.exit(0);
?}
?public?void?startWrite()

?
{
??new?WriteXmlFile(attrilist,elelist,rs,url).create(root,rootchild);
?}
}


文件?SaveAttrName.java?內(nèi)容:
package?currencycreatexml;

class?SaveAttrName


{
?private?String?attributename;
?private?int?index;
?public?SaveAttrName(String?attributename,int?index)

?
{
??this.attributename=attributename;
??this.index=index;
?}
?public?String?getAttributeName()

?
{
??return?attributename;
?}
?public?int?getIndex()

?
{
??return?index;
?}
}


文件?SaveEleName.java?內(nèi)容:
package?currencycreatexml;

class?SaveEleName


{
?private?String?elementname;
?private?int?index;
?public?SaveEleName(String?elementname,int?index)

?
{
??this.elementname=elementname;
??this.index=index;
?}
?public?String?getElementName()

?
{
??return?elementname;
?}
?public?int?getIndex()

?
{
??return?index;
?}
}


文件?WriteXmlFile.java?內(nèi)容:
package?currencycreatexml;

import?java.io.*;
import?java.sql.*;
import?java.util.ArrayList;
import?java.util.Iterator;
import?javax.xml.parsers.*;

import?org.w3c.dom.*;???????????????//使用dom解析
import?org.apache.crimson.tree.*;??//寫xml文件需要用到的jar包

class?WriteXmlFile


{
???private?ResultSet?rs;
???private?String?url;
???private?ArrayList?attrilist;
???private?ArrayList?elelist;
???public?WriteXmlFile(ArrayList?attrilist,ArrayList?elelist,ResultSet?rs,String?url)

???
{
??????this.attrilist=attrilist;
??????this.elelist=elelist;
??????this.rs=rs;
??????this.url=url;
???}

???/**?*//**
????*?@param?root?:?xml文件的根元素名
????*/
???public?void?create(String?root,String?rootchild)

???
{
??????DocumentBuilderFactory?dbf=DocumentBuilderFactory.newInstance();
??????Document?doc=null;
??????try

??????
{
?????????DocumentBuilder?db?=?dbf.newDocumentBuilder();
?????????doc?=?db.newDocument();
??????}?
??????catch?(ParserConfigurationException?e)

??????
{
?????????e.printStackTrace();
??????}
??????Element?rootelement=doc.createElement(root);
??????doc.appendChild(rootelement);?
??????Iterator?attri=attrilist.iterator();
??????Iterator?ele=elelist.iterator();
??????//System.out.println("iterator");
??????try

??????
{
?????????while(rs.next())

?????????
{
????????????Element?rootchildelement=doc.createElement(rootchild);
????????????//System.out.println("while");
????????????while(attri.hasNext())

????????????
{

???????????????/**?*//**
???????????????*??循環(huán)到屬性集合中取元素名稱,之后從查詢結(jié)果集中取得數(shù)據(jù),創(chuàng)建元素屬性
???????????????*/
???????????????SaveAttrName?temp=(SaveAttrName)attri.next();
???????????????rootchildelement.setAttribute(temp.getAttributeName(),rs.getString(temp.getIndex()).trim());
????????????}??
????????????rootelement.appendChild(rootchildelement);
????????????while(ele.hasNext())

????????????
{

???????????????/**?*//**
???????????????*??循環(huán)到元素集合中取元素名稱,之后從查詢結(jié)果集中取得數(shù)據(jù),創(chuàng)建結(jié)點(diǎn)
???????????????*/
???????????????SaveEleName?temp=(SaveEleName)ele.next();
???????????????Element?tempelement=doc.createElement(temp.getElementName());
???????????????Text?temptextelement=doc.createTextNode(rs.getString(temp.getIndex()).trim());
???????????????tempelement.appendChild(temptextelement);
???????????????rootchildelement.appendChild(tempelement);
????????????}
????????????attri=attrilist.iterator();?//重復(fù)循環(huán)到集合中取值,下同
????????????ele=elelist.iterator();
?????????}
??????}?
??????catch?(Exception?e)

??????
{
?????????e.printStackTrace();
??????}
??????writeXml(doc);
???}
???private?void?writeXml(Document?doc)

???
{
??????try

??????
{
?????????FileOutputStream?outStream?=?new?FileOutputStream(url);
?????????OutputStreamWriter?outWriter?=?new?OutputStreamWriter(outStream);
?????????((XmlDocument)doc).write(outWriter,?"GB2312");
?????????outWriter.close();
?????????outStream.close();
?????????System.out.print("\nWrite?xmlfile?successful!\n");
??????}?
??????catch?(Exception?e)

??????
{
?????????e.printStackTrace();
??????}?
???}
}


講解:
假設(shè)數(shù)據(jù)庫(kù)中有一個(gè)表,表名為“CCNUstudents”,
表中有幾條記錄,現(xiàn)將它轉(zhuǎn)換為xml文件,我轉(zhuǎn)換的規(guī)則為將記錄的關(guān)鍵字作為元素的屬性,其它作為了結(jié)點(diǎn)。當(dāng)然,你也可以以自己的規(guī)則將數(shù)據(jù)庫(kù)中的任何字段設(shè)置為屬性或元素。所有元素及屬性的名稱可以自定義,表中的內(nèi)容為:
?學(xué)號(hào)?????姓名??????學(xué)校?????????系別???????家庭住址?
20033274???邪????華中師范大學(xué)?信息管理系??湖北省監(jiān)利縣龔場(chǎng)鎮(zhèn)?
20043225??阿維???中南民族大學(xué)????經(jīng)濟(jì)系???湖北省監(jiān)利縣周老咀鎮(zhèn)?
假設(shè)關(guān)鍵字段為“學(xué)號(hào)”,則可將此表轉(zhuǎn)換為一個(gè)xml文件:
示例代碼如下:
文件?test.java?中測(cè)試函數(shù)的內(nèi)容:
public?static?void?main(String[]?args)?throws?Exception

?
{
??SQLExecute?conn=new?SQLExecute();
??ResultSet?rs=conn.sqlQuery("select?*?from?CCNUstudents");
??CreateXmlFile?create?=?new?CreateXmlFile(rs,"demo.xml");
??create.setRootElementName("學(xué)生基本信息","學(xué)生");
??create.setAttributeName("學(xué)生學(xué)號(hào)",1);
??create.setElementName("學(xué)生的姓名",2);
??create.setElementName("所在學(xué)校",3);
??create.setElementName("所在系",4);
??????????????????create.setElementName("住址",5);
??create.designOver();
?}

函數(shù)setAttributeName(String,int)及setElementName(String,int)中的String用來指定元素名或是屬性名,int型數(shù)字用來關(guān)聯(lián)此元素或?qū)傩缘闹凳菑谋碇械牡趲讉€(gè)字段取出來的。程序運(yùn)行結(jié)果后,就會(huì)在當(dāng)前目錄下生成一個(gè)名為“demo.xml”的xml文件,文件內(nèi)容為:
<?xml?version="1.0"?encoding="GB2312"?>

<學(xué)生基本信息>

<學(xué)生?學(xué)生學(xué)號(hào)="20033274">
<學(xué)生的姓名>邪</學(xué)生的姓名>
<所在學(xué)校>華中師范大學(xué)</所在學(xué)校>
<所在系>信息管理</所在系>
<住址>湖北省監(jiān)利縣龔場(chǎng)鎮(zhèn)</住址>
</學(xué)生>

<學(xué)生?學(xué)生學(xué)號(hào)="20043225">
<學(xué)生的姓名>阿維</學(xué)生的姓名>
<所在學(xué)校>中南民族大學(xué)</所在學(xué)校>
<所在系>經(jīng)濟(jì)系</所在系>
<住址>湖北省監(jiān)利縣周老咀鎮(zhèn)</住址>
</學(xué)生>

</學(xué)生基本信息>

當(dāng)然,如果數(shù)據(jù)表中有多個(gè)關(guān)鍵字,也可以在程序中指定多個(gè)屬性。
至此,一個(gè)xml文件就產(chǎn)生了。當(dāng)然,為求程序簡(jiǎn)便,關(guān)于xml文件中的命名空間、CDATA都沒有作處理。希望可以起到拋磚引玉的作用。
程序流程及邏輯可以參看類中的注釋~。
(-小邪(QQ:156411203,歡迎交流)-2006.7.21)。
最后補(bǔ)充一點(diǎn),你并不需要為每一個(gè)查詢結(jié)果集中的字段都指派相應(yīng)的元素或?qū)傩悦恍韪鶕?jù)你自己的需要,為你想要儲(chǔ)存到xml文件中的那些字段指派名稱就可以了,最后,別忘記了調(diào)用designOver()函數(shù),告訴程序表示已經(jīng)指派完畢了!^_^
注:本文轉(zhuǎn)自http://bbs.xml.org.cn/dispbbs.asp?boardID=17&replyID=75002&ID=41933&skin=1
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1473715