artist
artist

Reputation: 6371

Creating xml in Android

I am creating an xml file on external storage for app. I can write data to it but I would like the new data to be appended and not over written. Here's is the code so that writes to xml file but does not appends.I'd really appreciate your help.Thanks I am looking to create something like this.

<data>
<name>Name1</name>
<books>Book1</books>
<movies>Movie1</movies>
</data>
<data>
<name>Name2</name>
<books>Book2</books>
<movies>Movie2</movies>
</data>


 String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyApp" + "/MyAppData.xml";
                    fileos = new FileOutputStream(file_path);

                          xmlSerializer = Xml.newSerializer();
                          StringWriter writer = new StringWriter();
                          xmlSerializer.setOutput(writer);
                          xmlSerializer.startDocument("UTF-8", true);
                          xmlSerializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);

                          xmlSerializer.startTag(null, "data");
                          //tag date
                          xmlSerializer.startTag(null, "name");
                          xmlSerializer.text("Amber");
                          xmlSerializer.endTag(null, "name");

                          xmlSerializer.startTag(null, "books");
                          xmlSerializer.text(enter_books);
                          xmlSerializer.endTag(null, "books");

                          xmlSerializer.startTag(null, "movies");
                          xmlSerializer.text(enter_movies);
                          xmlSerializer.endTag(null, "movies");


                          xmlSerializer.endTag(null, "data");
                          xmlSerializer.flush();

                          String dataWrite = writer.toString();
                          fileos.write(dataWrite.getBytes());
                          fileos.close();

Upvotes: 0

Views: 65

Answers (1)

newhouse
newhouse

Reputation: 946

chage this:

fileos = new FileOutputStream(file_path);

by this

fileos = new FileOutputStream(file_path, true);

Reference: https://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html

Upvotes: 1

Related Questions