I just want to read xml file from the sdcard and want to add some new data into it. Then I have to write it into sdcard. How can I implement it. Any help will be highly appreciated.
Asked
Active
Viewed 1,575 times
0
-
Iam able to read and write data but iam unable to append data to existed xml file.Could you help me :) – Narender Reddy Nov 03 '16 at 11:44
2 Answers
0
Reading and Writing XML files sample is given here. And you can get plenty of them using google. However two important classes for this are XmlSerializer
and XmlPullParser
. A simple XmlSerializer example is:-
public static String CreateXMLString() throws IllegalArgumentException, IllegalStateException, IOException
{
XmlSerializer xmlSerializer = Xml.newSerializer();
StringWriter writer = new StringWriter();
xmlSerializer.setOutput(writer);
//Start Document
xmlSerializer.startDocument("UTF-8", true);
xmlSerializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
//Open Tag <file>
xmlSerializer.startTag("", "file");
XmlSerializer tempXml = Xml.newSerializer();
StringWriter tempWriter = new StringWriter();
xmlSerializer.startTag("", "something");
xmlSerializer.attribute("", "ID", "000001");
xmlSerializer.startTag("", "name");
xmlSerializer.text("CO");
xmlSerializer.endTag("", "name");
xmlSerializer.endTag("", "something");
//end tag <file>
xmlSerializer.endTag("", "file");
xmlSerializer.endDocument();
return writer.toString();
}
Similarly You can find one for XmlPullParser as well. Since you want to read / write files in sdCard don't forget to call Environment.getExternalStorageDirectory().getAbsolutePath()
to obtain the Path of external Storage directory. See a simple example here.
Don't forget to add following line to your manifest file without that nothing will work.
If You don't get that working. Update your question with the specific problem.