Domanda

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 is StAX(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("mail@gmail.com");
 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

È stato utile?

Soluzione

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top