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,这是不是有问题
生成xml文件性能对比
生成xml性能对比
生成xml的四种方式的性能对比:SAX > DOM4J > JDOM > DOM
在一个课程测试用例中四种方式的用时(Ms):
DOM:418
SAX:3
JDOM:158
DOM4J:59
生成xml的四种方式的性能对比:SAX > DOM4J > JDOM > DOM
在一个课程测试用例中四种方式的用时(Ms):
DOM:418
SAX:3
JDOM:158
DOM4J:59
DOM/SAX官方自带:
DOM生成DOM树加载到内存,方便修改,性能慢。
SAX性能最高,但是不能返回修改便签中的内容
DOM4J相对综合最好,应用广泛