java - Why does StAX create XML slower than DOM? -


i trying measure time needed stax , dom create 2 same documents. dont know why dom faster in creating xml. maybe code stax writer not good. here 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");           (int = 1;  <= 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 = 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 stax way faster in reading document, can´t explain this. except have pretty bad code stax

thanks

add bufferedouputstream here

xmlstreamwriter w = f.createxmlstreamwriter(new bufferedoutputstream(new fileoutputstream(subor)), "utf-8"); 

now test speed , see stax @ least 2 times faster


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -