标签:java redis shiro maven druid
Java是一门高级编程语言,自然少不了对各种文件进行操作的功能。现在笔者就来写一个操作XML的demo供大家参考和学习。首先要知道Java对XML操作的方式并非只有一种,那么究竟哪种方式更高效,哪种方式更适用或主流呢?Java主要有四种方式操作XML文件,下面我们就用代码下载来看一看,比较一下。
以下是Java创建XML文件的四种主流方式。
public class CreateTest {
/**
* DOM方式生成xml文档
*/
private void DOMCreateXML() {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
try {
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
Document document = db.newDocument();
document.setXmlStandalone(true);
Element bookstore = document.createElement("bookStore");
Element book = document.createElement("book");
Element name = document.createElement("name");
name.setTextContent("小王子");
book.appendChild(name);
book.setAttribute("id", "1");
bookstore.appendChild(book);
document.appendChild(bookstore);
TransformerFactory tff = TransformerFactory.newInstance();
try {
Transformer tf = tff.newTransformer();
tf.setOutputProperty(OutputKeys.INDENT, "yes");
tf.transform(new DOMSource(document), new StreamResult(new File(
"books1.xml")));
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
/**
* SAX方式生成xml文档
*/
public void SAXCreateXML() {
SAXTransformerFactory tff = (SAXTransformerFactory) SAXTransformerFactory
.newInstance();
try {
TransformerHandler handler = tff.newTransformerHandler();
Transformer tr = handler.getTransformer();
tr.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tr.setOutputProperty(OutputKeys.INDENT, "yes");
File f = new File("books2.xml");
if (!f.exists()) {
f.createNewFile();
}
Result result = new StreamResult(new FileOutputStream(f));
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);
handler.characters("小王子".toCharArray(), 0, "小王子".length());
handler.endElement("", "", "name");
handler.endElement("", "", "book");
handler.endElement("", "", "bookstore");
handler.endDocument();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}
/**
* JDOM方式生成xml文档
*/
private void JDOMCreateXML() {
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.getCompactFormat();
format.setIndent("");
format.setEncoding("UTF-8");
XMLOutputter outputer = new XMLOutputter(format);
try {
outputer.output(document, new FileOutputStream(new File("books3.xml")));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* DOM4J方式生成xml文档
*/
private void DOM4JCreateXML(){
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 format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
File file = new File("books4.xml");
XMLWriter writer;
try {
writer = new XMLWriter(new FileOutputStream(file), format);
writer.setEscapeText(false);
writer.write(document);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
} 下载
@Test
public void testPerformance() throws Exception{
System.out.println("性能测试:");
//测试DOM的性能:
long start = System.currentTimeMillis();
DOMCreateXML();
System.out.println("DOM:"+ (System.currentTimeMillis() - start));
//测试SAX的性能:
start = System.currentTimeMillis();
SAXCreateXML();
System.out.println("SAX:"+ (System.currentTimeMillis() - start) );
//测试JDOM的性能:
start = System.currentTimeMillis();
JDOMCreateXML();
System.out.println("JDOM:"+ (System.currentTimeMillis() - start) );
//测试DOM4J的性能:
start = System.currentTimeMillis();
DOM4JCreateXML();
System.out.println("DOM4J:"+ (System.currentTimeMillis() - start) );
}
}
现在笔者贴出控制台输出信息:
性能测试:
DOM:68
SAX:4
JDOM:78
DOM4J:60
由此可得出创建XML文件的速度排名:SAX下载 > DOM4J > DOM > JDOM
在速度上我们明显可以看出SAX的巨大优势,其次是DOM4J 和DOM ,在性能上笔者推荐使用SAX,其次可以考虑DOM4J 和DOM,而JDOM性能上劣势较为明显,不建议使用
另外,作为一名程序员(开发者),代码量和代码复杂度也是值得考量的因素。在这一点上笔者推荐使用DOM4J
综合以上两点,笔者推荐使用SAX和DOM4J进行对XML文件的操作更为适用,而且这两种方式也是被大众所推崇的主流方式。
但是,以上代码分析仅仅是从创建XML文件上作出的简单分析,而且是创建一个很简单的数据量极小的XML文件。所以并不能给出总结性的结论说明哪种方式一定是最好的
标签:java redis shiro maven druid
原文地址:http://12465640.blog.51cto.com/12455640/1886765