Java四種生成xml檔案的方法

2021-07-11 13:13:52 字數 3086 閱讀 6506

1,dom

//建立乙個documentbuilde***ctory物件

documentbuilde***ctory dbf =

documentbuilde***ctory.newinstance();

//建立乙個documentbuilder物件

try catch (parserconfigurationexception e) catch (transformerconfigurationexception e) catch (transformerexception e)

2,sax

//1,建立 乙個transforme***ctory的類的物件

saxtransforme***ctory tff = (saxtransforme***ctory) saxtransforme***ctory.newinstance();

try

//6,建立resulet物件,並且是的與handler關聯

result result = new streamresult(new fileoutputstream(file));

handler.setresult(result);

//7.利用handler物件進行xml檔案內容的編寫

//開啟document

handler.startdocument();

attributesimpl attr = new attributesimpl();

handler.startelement("", "", "bookstart", attr);

attr.clear();

for(){} 在for迴圈中新增資料。對了list數值進行遍歷

//新增一本書

attr.addattribute("","","id","","1");

handler.startelement("", "", "book", attr);

//建立name節點

attr.clear();

handler.startelement("", "", "name", attr);

handler.characters("你是我的女神"

.tochararray(),0,"你是我的女神"

.length());

handler.endelement("","","name");

// 。。。。。

handler.endelement("","","book");

handler.endelement("","","bookstart");

//關閉

handler.enddocument();

} catch (transformerconfigurationexception e) catch (filenotfoundexception e) catch (ioexception e) catch (saxexception e)

3,dom4j

//1,建立 document物件,代表整個xml文件

document document = documenthelper.createdocument();

//2,建立 根節點

element rss = document.addelement("rss");

//向rrs中新增屬性

rss.addattribute("version", "2.0");

//3,生成子節點和節點內容

element channel = rss.addelement("channel");

element title = channel.addelement("title");

title.settext("國內新聞");

//要在下面設定是否轉義

// title.settext("");

//設定xml的格式

outputformat format =

outputformat.createprettyprint();

// format.setencoding("gbk");設定編碼格式

//4,生成xml檔案

file file = new file("rss.xml");

xmlwriter write = null;

try catch (ioexception e)

4,jdom

//生成乙個根節點

element rss = new element("rss");

rss.setattribute("version", "2.0");

//生成乙個document物件

document document = new document(rss);

element channel = new element("channel");

rss.addcontent(channel);

element title = new element("title");

// title.settext("國內最新新聞");

title.settext("");

channel.addcontent(title);

format format = format.getcompactformat();

format.setindent("");

format.setencoding("utf-8");

// format.settextmode(format.textmode.trim);

// format.setomitencoding(false);

// format.setspecifiedattributesonly(true);

//4,建立xmloutputer物件

xmloutputter outputer = new xmloutputter();

//5.利用outputer物件,將document轉化成乙個流

try catch (ioexception e)

Java中四種XML解析技術

在平時工作中,難免會遇到把 xml 作為資料儲存格式。面對目前種類繁多的解決方案,哪個最適合我們呢?在這篇文章中,我對這四種主流方案做乙個不完全評測,僅僅針對遍歷 xml 這塊來測試,因為遍歷 xml 是工作中使用最多的 至少我認為 預 備 測試環境 amd 毒龍1.4g oc 1.5g 256m ...

Java中四種XML解析技術

自 http www.it.com.cn f edu 053 27 93819.htm 在平時工作中,難免會遇到把 xml 作為資料儲存格式。面對目前種類繁多的解決方案,哪個最適合我們呢?在這篇文章中,我對這四種主流方案做乙個不完全評測,僅僅針對遍歷 xml 這塊來測試,因為遍歷 xml 是工作中使...

Java中四種XML解析技術

在平時工作中,難免會遇到把 xml 作為資料儲存格式。面對目前種類繁多的解決方案,哪個最適合我們呢?在這篇文章中,我對這四種主流方案做乙個不完全評測,僅僅針對遍歷 xml 這塊來測試,因為遍歷 xml 是工作中使用最多的 至少我認為 預 備 測試環境 amd 毒龍1.4g oc 1.5g 256m ...