現(xiàn)有以下XML文檔books.xml,下面的示例生成此文檔部分內(nèi)容
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book id="1">
<name>冰與火之歌</name>
<author>喬治馬丁</author>
<year>2014</year>
<price>89</price>
</book>
<book id="2">
<name>安徒生童話</name>
<year>2004</year>
<price>77</price>
<language>English</language>
</book>
</bookstore>
DOM
1飘千、創(chuàng)建DocumentBuilder對(duì)象
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
2堂鲜、添加節(jié)點(diǎn)
document.setXmlStandalone(true);
Element bookstore = document.createElement("bookStore");
//向bookstore根節(jié)點(diǎn)中添加子節(jié)點(diǎn)book
Element book = document.createElement("book");
Element name = document.createElement("name");
name.setTextContent("???");
book.appendChild(name);
book.setAttribute("id", "1");
//將book節(jié)點(diǎn)添加到bookstore根節(jié)點(diǎn)中
bookstore.appendChild(book);
//將bookstore節(jié)點(diǎn)(已經(jīng)包含了book)添加到dom樹(shù)中
document.appendChild(bookstore);
3、生成xml文件
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();
//設(shè)置文件
tf.setOutputProperty(OutputKeys.INDENT, "yes");
tf.transform(new DOMSource(document),new StreamResult(new File("books1.xml")));
SAX
1护奈、創(chuàng)建一個(gè)TransformerFactory類的對(duì)象
SAXTransformerFactory tff = (SAXTransformerFactory) SAXTransformerFactory.newsInstance();
2缔莲、通過(guò)SAXTransformerFactory對(duì)象創(chuàng)建一個(gè)TransformerHandler對(duì)象
TransformerHandler handler = tff.newTransformerHandler();
3、通過(guò)handler對(duì)象創(chuàng)建一個(gè)Transformer對(duì)象
Transformer tr = handler.getTransformer();
4霉旗、通過(guò)Transformer對(duì)象對(duì)生成的xml文件進(jìn)行設(shè)置
// 設(shè)置xml的編碼
tr.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
// 設(shè)置xml的“是否換行”
tr.setOutputProperty(OutputKeys.INDENT, "yes");
5痴奏、創(chuàng)建一個(gè)Result對(duì)象
```java
File f = new File("newbooks.xml");
if (!f.exists()) {
f.createNewFile();
}
6蛀骇、創(chuàng)建Result對(duì)象,并且使其與handler關(guān)聯(lián)
Result result = new StreamResult(new FileOutputStream(f));
handler.setResult(result);
7读拆、利用handler對(duì)象進(jìn)行xml文件內(nèi)容的編寫(xiě)O
// 打開(kāi)document
handler.startDocument();
8擅憔、添加節(jié)點(diǎn)屬性和節(jié)點(diǎn)值
AttributesImpl attr = new AttributesImpl();
handler.startElement("", "", "bookstore", attr);
for (Book book : bookList) {
attr.clear();
attr.addAttribute("", "", "id", "", book.getId());
handler.startElement("", "", "book", attr);
// 創(chuàng)建name節(jié)點(diǎn)
if (book.getName() != null && !book.getName().trim().equals("")) {
attr.clear();
handler.startElement("", "", "name", attr);
handler.characters(book.getName().toCharArray(), 0, book
.getName().length());
handler.endElement("", "", "name");
}
handler.endElement("", "", "book");
}
handler.endElement("", "", "bookstore");
// 關(guān)閉document
handler.endDocument();
JDOM
1.生成一個(gè)根節(jié)點(diǎn)
Element rss = new Element("rss");
2.為節(jié)點(diǎn)添加屬性
rss.setAttribute("version", "2.0");
3.生成一個(gè)document對(duì)象
Document document = new Document(rss);
Element channel = new Element("channel");
rss.addContent(channel);
Element title = new Element("title");
title.setText("<![CDATA[上海移動(dòng)互聯(lián)網(wǎng)產(chǎn)業(yè)促進(jìn)中心正式揭牌 ]]>");
channel.addContent(title);
//設(shè)置文件編碼和換行
Format format = Format.getCompactFormat();
format.setIndent("");
format.setEncoding("GBK");
4.創(chuàng)建XMLOutputter的對(duì)象
XMLOutputter outputer = new XMLOutputter(format);
5.利用outputer將document對(duì)象轉(zhuǎn)換成xml文檔
outputer.output(document, new FileOutputStream(new File("rssnews.xml")));
DOM4J
使用DOM4J生成RSS文件
1.創(chuàng)建document對(duì)象,代表整個(gè)xml文檔
Document document = DocumentHelper.createDocument();
2.創(chuàng)建根節(jié)點(diǎn)rss
Element rss = document.addElement("rss");
3.向rss節(jié)點(diǎn)中添加version屬性
rss.addAttribute("version", "2.0");
4.生成子節(jié)點(diǎn)及節(jié)點(diǎn)內(nèi)容
Element channel = rss.addElement("channel");
Element title = channel.addElement("title");
title.setText("<![CDATA[上海移動(dòng)互聯(lián)網(wǎng)產(chǎn)業(yè)促進(jìn)中心正式揭牌 ]]>");
5.設(shè)置生成xml的格式
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("GBK");
6.生成xml文件
File file = new File("rssnews.xml");
XMLWriter writer;
writer = new XMLWriter(new FileOutputStream(file), format);
//設(shè)置是否轉(zhuǎn)義檐晕,默認(rèn)值是true暑诸,代表轉(zhuǎn)義
writer.setEscapeText(false);
writer.write(document);
writer.close();