0

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.

Aju
  • 4,597
  • 7
  • 35
  • 58

2 Answers2

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.

Community
  • 1
  • 1
Amit
  • 13,134
  • 17
  • 77
  • 148
0

You just to get the File using

File file = new File(Environment.getExternalStorageDirectory()
                + "your_path/your_xml.xml");

after you can do xml parsing to read that file.

NightFury
  • 13,436
  • 6
  • 71
  • 120
Priya
  • 1,763
  • 1
  • 12
  • 11