本文介绍了为什么StAX创建XML比DOM慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试测量StAX和DOM创建两个相同文档所需的时间。我不知道为什么DOM在创建XML时更快。也许我的StAX作家的代码不是很好。
所以这里是
StAX(更长的代码)
public static final int pocet = 100000;
try {
String encoding =UTF-8;
XMLOutputFactory f = XMLOutputFactory.newInstance();
XMLStreamWriter w = f.createXMLStreamWriter(
new FileOutputStream(subor),
encoding);
w.writeStartDocument(encoding,1.0);
w.writeCharacters(\r\\\
);
w.writeStartElement(Noviny); (int i = 1; i< = pocet; i ++){
w.writeCharacters(\r\\\
);
w.writeStartElement(Autor);
w.writeCharacters(\r\\\
);
w.writeStartElement(Id);
String ID = Integer.toString(i);
w.writeCharacters(ID);
w.writeEndElement();
w.writeCharacters(\r\\\
);
w.writeStartElement(Meno);
w.writeCharacters(Autor+ i);
w.writeEndElement();
w.writeCharacters(\r\\\
);
w.writeStartElement(Email);
w.writeCharacters(Autor+ i +@ email.com);
w.writeEndElement();
w.writeCharacters(\r\\\
);
w.writeStartElement(Tel_cislo);
w.writeAttribute(typ,pevna);
w.writeCharacters(+ 4219);
w.writeEndElement();
w.writeCharacters(\r\\\
);
w.writeStartElement(plat);
w.writeCharacters(5000);
w.writeEndElement();
w.writeCharacters(\r\\\
);
w.writeEndElement();
w.writeCharacters(\r\\\
);
}
w.writeCharacters(\r\\\
);
w.writeEndElement();
w.writeCharacters(\r\\\
);
w.writeEndDocument();
w.close();
}
catch(异常e){
e.printStackTrace();
}
DOM
int pocet = 1500000;
try {
DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dfactory.newDocumentBuilder();
DOMImplementation domImpl = builder.getDOMImplementation();
文档文档= domImpl.createDocument(null,Noviny,null);
Node noviny = document.getDocumentElement(); (int i = 0; i< pocet; i ++){
元素autor = document.createElement(autor);
元素meno = document.createElement(meno);
元素id = document.createElement(id);
元素email = document.createElement(email);
元素tel = document.createElement(tel_cislo);
元素plat = document.createElement(plat);
文本textid = document.createTextNode(+ i);
文本textmeno = document.createTextNode(Autor+ i);
文本textemail = document.createTextNode([email protected]);
Text texttel = document.createTextNode(65456465465);
文本textplat = document.createTextNode(200);
noviny.appendChild(autor);
autor.appendChild(id);
id.appendChild(textid);
autor.appendChild(meno);
meno.appendChild(textmeno);
autor.appendChild(email);
email.appendChild(textemail);
autor.appendChild(tel);
tel.appendChild(texttel);
autor.appendChild(plat);
plat.appendChild(textplat);
}
TransformerFactory factory = TransformerFactory.newInstance();
变压器变压器= factory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT,yes);
transformer.setOutputProperty(OutputKeys.ENCODING,UTF-8);
transformer.transform(new DOMSource(document),new StreamResult(new File(subor)));
我知道StAX在阅读文档时速度更快,但我不能解释这一点。除了我对StAX有不错的代码
谢谢
解决方案
在这里添加BufferedOuputStream
XMLStreamWriter w = f.createXMLStreamWriter(new BufferedOutputStream(new FileOutputStream(subor)),UTF-8) ;
现在测试速度,你会看到StAX至少快2倍。 b $ b
i was trying to measure time needed for StAX and DOM to create two same documents. I dont know why DOM is faster in creating XML. Maybe my code for StAX writer is not very good.so here it isStAX(longer code)
public static final int pocet =100000;
try {
String encoding = "UTF-8";
XMLOutputFactory f = XMLOutputFactory.newInstance();
XMLStreamWriter w = f.createXMLStreamWriter(
new FileOutputStream(subor),
encoding);
w.writeStartDocument(encoding, "1.0");
w.writeCharacters("\r\n");
w.writeStartElement("Noviny");
for (int i = 1; i <= pocet; i++) {
w.writeCharacters("\r\n ");
w.writeStartElement("Autor");
w.writeCharacters("\r\n ");
w.writeStartElement("Id");
String ID = Integer.toString(i);
w.writeCharacters(ID);
w.writeEndElement();
w.writeCharacters("\r\n ");
w.writeStartElement("Meno");
w.writeCharacters("Autor"+i);
w.writeEndElement();
w.writeCharacters("\r\n ");
w.writeStartElement("Email");
w.writeCharacters("Autor"+i+"@email.com");
w.writeEndElement();
w.writeCharacters("\r\n ");
w.writeStartElement("Tel_cislo");
w.writeAttribute("typ", "pevna");
w.writeCharacters("+4219");
w.writeEndElement();
w.writeCharacters("\r\n ");
w.writeStartElement("plat");
w.writeCharacters("5000");
w.writeEndElement();
w.writeCharacters("\r\n ");
w.writeEndElement();
w.writeCharacters("\r\n");
}
w.writeCharacters("\r\n");
w.writeEndElement();
w.writeCharacters("\r\n");
w.writeEndDocument();
w.close();
}
catch (Exception e) {
e.printStackTrace();
}
DOM
int pocet =1500000;
try {
DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dfactory.newDocumentBuilder();
DOMImplementation domImpl = builder.getDOMImplementation();
Document document = domImpl.createDocument(null, "Noviny", null);
Node noviny = document.getDocumentElement();
for(int i = 0; i<pocet ; i++) {
Element autor = document.createElement("autor");
Element meno = document.createElement("meno");
Element id = document.createElement("id");
Element email = document.createElement("email");
Element tel = document.createElement("tel_cislo");
Element plat = document.createElement("plat");
Text textid = document.createTextNode(""+i);
Text textmeno = document.createTextNode("Autor"+i);
Text textemail = document.createTextNode("[email protected]");
Text texttel = document.createTextNode("65456465465");
Text textplat = document.createTextNode("200");
noviny.appendChild(autor);
autor.appendChild(id);
id.appendChild(textid);
autor.appendChild(meno);
meno.appendChild(textmeno);
autor.appendChild(email);
email.appendChild(textemail);
autor.appendChild(tel);
tel.appendChild(texttel);
autor.appendChild(plat);
plat.appendChild(textplat);
}
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.transform(new DOMSource(document),new StreamResult(new File(subor)));
i know that StAX is way faster in reading document, but i can´t explain this. Except that i have pretty bad code for StAX
thanks
解决方案
Add BufferedOuputStream here
XMLStreamWriter w = f.createXMLStreamWriter(new BufferedOutputStream(new FileOutputStream(subor)), "UTF-8");
now test the speed and you will see that StAX is at least 2 times faster
这篇关于为什么StAX创建XML比DOM慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!