java - Append to an XML file -


i writing data xml file.

here code:

    try {      documentbuilderfactory docfactory = documentbuilderfactory.newinstance();     documentbuilder docbuilder = docfactory.newdocumentbuilder();      // root elements     document doc = docbuilder.newdocument();     element rootelement = doc.createelement("company");     doc.appendchild(rootelement);      // staff elements     element staff = doc.createelement("staff");     rootelement.appendchild(staff);      // set attribute staff element     attr attr = doc.createattribute("id");     attr.setvalue("1");     staff.setattributenode(attr);      // shorten way     // staff.setattribute("id", "1");      // firstname elements     element firstname = doc.createelement("firstname");     firstname.appendchild(doc.createtextnode("yong"));     staff.appendchild(firstname);      // lastname elements     element lastname = doc.createelement("lastname");     lastname.appendchild(doc.createtextnode("mook kim"));     staff.appendchild(lastname);      // nickname elements     element nickname = doc.createelement("nickname");     nickname.appendchild(doc.createtextnode("mkyong"));     staff.appendchild(nickname);      // salary elements     element salary = doc.createelement("salary");     salary.appendchild(doc.createtextnode("100000"));     staff.appendchild(salary);      // write content xml file     transformerfactory transformerfactory = transformerfactory.newinstance();     transformer transformer = transformerfactory.newtransformer();     domsource source = new domsource(doc);     streamresult result = new streamresult(new file("file.xml"));      // output console testing     // streamresult result = new streamresult(system.out);      transformer.transform(source, result);      system.out.println("file saved!");    } catch (parserconfigurationexception pce) {     pce.printstacktrace();   } catch (transformerexception tfe) {     tfe.printstacktrace();   } } 

this code writes xml data "file.xml". if have "file.xml" file, best way append xml data file? need rewrite whole above code, or easy adapt code?

yes - you're dealing dom, have have whole file in memory. alternative stax.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -