package com.imooc.xmlTest; import org.dom4j.DocumentHelper; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; import org.jdom2.output.Format; import org.jdom2.output.XMLOutputter; import org.testng.annotations.Test; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.xml.sax.SAXException; import org.xml.sax.helpers.AttributesImpl; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.OutputKeys; import javax.xml.transform.Result; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.sax.SAXTransformerFactory; import javax.xml.transform.sax.TransformerHandler; import javax.xml.transform.stream.StreamResult; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class JUnitTest1 { public void DOMCreateXML() throws ParserConfigurationException, TransformerException { DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance(); DocumentBuilder db=dbf.newDocumentBuilder(); Document document=db.newDocument(); document.setXmlStandalone(true); Element bookstore=document.createElement("bookstore"); Element book=document.createElement("book"); book.setAttribute("id","1"); Element name=document.createElement("name"); name.setTextContent("小王子"); book.appendChild(name); bookstore.appendChild(book); document.appendChild(bookstore); TransformerFactory tff=TransformerFactory.newInstance(); Transformer tf=tff.newTransformer(); tf.setOutputProperty(OutputKeys.INDENT,"yes"); tf.transform(new DOMSource(document),new StreamResult(new File("demo2/books.xml"))); } public void SAXCreateXML() throws ParserConfigurationException, IOException, SAXException, TransformerConfigurationException { SAXTransformerFactory tff=(SAXTransformerFactory)SAXTransformerFactory.newInstance(); TransformerHandler handler=tff.newTransformerHandler(); Transformer tf=handler.getTransformer(); tf.setOutputProperty(OutputKeys.ENCODING,"UTF-8"); tf.setOutputProperty(OutputKeys.INDENT,"yes"); Result result=new StreamResult(new FileOutputStream(new File("demo2/books1.xml"))); handler.setResult(result); handler.startDocument(); AttributesImpl attr=new AttributesImpl(); handler.startElement("","","bookstore",attr); attr.clear(); attr.addAttribute("","","id","","1"); handler.startElement("","","book",attr); attr.clear(); handler.startElement("","","name",attr); String name="小王子"; handler.characters(name.toCharArray(),0,name.length()); handler.endElement("","","name"); handler.endElement("","","book"); handler.endElement("","","bookstore"); handler.endDocument(); } public void JDOMCreateXML() throws IOException { org.jdom2.Element bookstore=new org.jdom2.Element("bookstore"); org.jdom2.Document document=new org.jdom2.Document(bookstore); org.jdom2.Element book=new org.jdom2.Element("book"); book.setAttribute("id","1"); org.jdom2.Element name=new org.jdom2.Element("name"); name.setText("小王子"); book.addContent(name); bookstore.addContent(book); Format format=Format.getPrettyFormat(); format.setEncoding("gbk"); XMLOutputter xmlOutputter=new XMLOutputter(format); xmlOutputter.output(document,new FileOutputStream("demo2/books2.xml")); } public void DOM4JCreateXML() throws IOException { org.dom4j.Document document= DocumentHelper.createDocument(); org.dom4j.Element bookstore=document.addElement("bookstore"); org.dom4j.Element book=bookstore.addElement("book"); book.addAttribute("id","1"); org.dom4j.Element name=book.addElement("name"); name.setText("小王子"); OutputFormat outputFormat=OutputFormat.createPrettyPrint(); XMLWriter xmlWriter=new XMLWriter(new FileOutputStream("demo2/books3.xml"),outputFormat); xmlWriter.write(document); xmlWriter.close(); } @Test public void testPerformance() throws Exception{ System.out.println("性能测试:"); long start=System.currentTimeMillis(); DOMCreateXML(); System.out.println("DOM:"+(System.currentTimeMillis()-start)); start=System.currentTimeMillis(); SAXCreateXML(); System.out.println("SAX:"+(System.currentTimeMillis()-start)); start=System.currentTimeMillis(); JDOMCreateXML(); System.out.println("JDOM:"+(System.currentTimeMillis()-start)); start=System.currentTimeMillis(); DOM4JCreateXML(); System.out.println("DOM4J:"+(System.currentTimeMillis()-start)); } }
SAX的测试结果为0,这是不是有问题
JDOM 设置 XML 格式
format.setIndent("");//这样写已经默认换行了,这里面不需要再写\n,否则就换两行了。
Element title = new Element("title");
title.setContent(new CDATA("上海移动互联网产业促进中心正式揭牌"));//设置转义,默认自带CDATA标签
public void createXML() {
ArrayList<Book> books = parserXML();
//生成XML
//1、创建一个TransformerFactory类的对象
SAXTransformerFactory tff = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
try {
//通过SAXTransformerFactory对象创建一个TransformerHandler对象
TransformerHandler handler = tff.newTransformerHandler();
//3、通过Handler对象创建一个Transformer对象
Transformer tr = handler.getTransformer();
//4、通过Transformer对象生成的xml文件进行设置:设置xml的编码;设置xml是否换行
tr.setOutputProperty(OutputKeys.ENCODING, "utf-8");
tr.setOutputProperty(OutputKeys.INDENT, "yes");
//5、创建一个Result对象
File file = new File("src/res/newbooks.xml");
if (file.exists()) {
file.createNewFile();
}
//创建Result对象,并且使其与handler关联
Result result = new StreamResult(new FileOutputStream(file));
handler.setResult(result);
//7、利用handler对象进行xml文件内容的编写
//打开document
handler.startDocument();
AttributesImpl attr = new AttributesImpl();
handler.startElement("", "", "bookstore", attr);
for (Book book : books) {
attr.clear();
attr.addAttribute("", "", "id", "", book.getId());
handler.startElement("", "", "book", attr);
//创建name节点
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");
}
//创建year节点
if (book.getYear() != null && !book.getYear().trim().equals("")) {
attr.clear();
handler.startElement("", "", "year", attr);
handler.characters(book.getYear().toCharArray(), 0, book.getYear().length());
handler.endElement("", "", "year");
}
//创建author节点
if (book.getAuthor() != null && !book.getAuthor().trim().equals("")) {
attr.clear();
handler.startElement("", "", "author", attr);
handler.characters(book.getAuthor().toCharArray(), 0, book.getAuthor().length());
handler.endElement("", "", "author");
}
//创建price节点
if (book.getPrice() != null && !book.getPrice().trim().equals("")) {
attr.clear();
handler.startElement("", "", "price", attr);
handler.characters(book.getPrice().toCharArray(), 0, book.getPrice().length());
handler.endElement("", "", "price");
}
//创建language节点
if (book.getLanguage() != null && !book.getLanguage().trim().equals("")) {
attr.clear();
handler.startElement("", "", "language", attr);
handler.characters(book.getLanguage().toCharArray(), 0, book.getLanguage().length());
handler.endElement("", "", "language");
}
handler.endElement("", "", "book");
}
handler.endElement("", "", "bookstore");
//关闭document
handler.endDocument();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}
生成XML文件的四种方式:DOM,SAX,DOM4J、JDOM
后两种方法会生成一个全新形式的文件——RSS
生成xml文件性能对比
四种生成xml方式对比
常用的节点类型
生成xml性能对比
四种生成xml方式对比
DOM基于tree ,生成的DOM树保存在内存中,方便重新排列、删除、修改
SAX基于事件,生成后无法回头修改
JDOM、DOM4J基于底层API
JDOM设置XML格式
JDOM添加子节点及节点间文本
JDOM生成XML根节点
处理转义字符
转义代表< 、>符号在xml中进行转义为<>
XMLWriter的setEscapeText(false) 设置是否转义,默认值true,代表转义
生成子节点和内容并设置换行
生成RSS根节点以version属性
RSS是用来描述和同步网站内容的格式,本质是xml
characters() 在xml标签之间添加文本,第一个参数是字符串的char数组,使用toCharArray()转化,第二个参数是字符数组的起始位置,第二个参数是从起始位置开始截取的长度。
注意:如果将setResult方法放在startDocument之后会报错。
AttributesImpl的clear()方法清除属性 ,addAttribute()前两个参数与命名空间有关,第三个参数是属性名,第四个参数是属性类型,第五个参数是属性值
startDocument() 打开document
endDocument() 关闭document
startElement()前两个参数与命名空间有关,第三个参数代表标签名称,第四个参数是属性集AttributesImpl对象
endElement()前两个参数与命名空间有关,第三个参数代表标签名称
注意:setOutProperty要生效必须在setResult方法之前。
SAX生成XML准备工作
Document的setXmlStandalone(true) 可以在xml首行中除去standalone属性
设置xml的值不应使用setNodeValue("") 应该使用setTextContent("")
使用Transormer的transform方法将Document对象转化为xml文件
setOutputProperty(OutputKeys.INDENT,"yes")方法可以设置换行
使用DocumentBuilderFactory创建DocumentBuilder对象
使用DocumentBuilder对象生成Document对象,调用Document对象的createElement()方法生成节点, setAttribute("","")方法添加节点属性,再调用appendChild()方法将节点添加到xml中。
1.根节点element,添加属性 setAttribute()
2.文件document对象
3.XMLOutputter对象 outputter.output(document,文件);通过输出流转换为xml文件
1.创建document doc = DocumentHelper.createDocument();
2.创建根节点rss doc.addelement
3.添加根节点属性 rss.addAtribute("","");
4.生成xml文件 通过XMLWriter生成;
生成xml的四种方式的性能对比:SAX > DOM4J > JDOM > DOM
在一个课程测试用例中四种方式的用时(Ms):
DOM:418
SAX:3
JDOM:158
DOM4J:59
四种生成xml方式对比
设置输出格式和属性
Format format = Format.getCompactFormat(); format.setIndent(""); new XMLOutputter(format);//将format作为参数传入
JDOM生成xml文档
Element rss = new Element("rss"); rss.setAttribute("version","2.0"); Document document = new Document(rss); XMLOutputter outputter = new XMLOutputter(); outputter.output(document,new FileOutputStream("newRSS.xml"));
避免生成xml中特殊字符自动转义:
writer.setEscapeText(false);