java - Appending data to an XML file using OutputStreamWriter (StAX parser) -


i want append data xml file using outputstreamwriter ending below

<?xml version="1.0" encoding="utf-8"?> <doc>  <msg>    <tag1>data1</tag1>    ...  </msg> </doc> <?xml version="1.0" encoding="utf-8"?> <doc>  <msg>    <tag1>data2</tag1>    ...  </msg> </doc> 

how can achieve correct format

<?xml version="1.0" encoding="utf-8"?> <doc>  <msg>    <tag1>data1</tag1>    ...  </msg>  <msg>    <tag1>data2</tag1>    ...  </msg> </doc> 

i have tried below code

 xmloutputfactory outputfactory = xmloutputfactory.newinstance();     file file = new file(outputfile);     writer fw = null;     xmlstreamwriter w = null;      try {         if(!file.isfile()) {             file.createnewfile();         }         fw = new outputstreamwriter(new fileoutputstream(file, true), "utf-8");          w = outputfactory.createxmlstreamwriter(fw);         w.writestartdocument("utf-8", "1.0");         w.writestartelement("doc");          createmessageelement(fingerprint, w, data);//method write data          w.writeendelement();         w.writeenddocument();          w.flush();         w.close();          w.close();          fw.flush();         fw.close(); 

please give me suggestion. i'm new stax.

your example appending new xml document end of file. if want append data existing xml doc have read source xml, , write file, inserting new element need.... see http://docs.oracle.com/javase/tutorial/jaxp/stax/example.html#bnbgq example using stax api.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -