简体   繁体   中英

How to edit XML in Android and save?

I have an XML file, as

<?xml version="1.0" encoding="UTF-8"?>

<TODO-LIST>

<MYTASK TIME = "10:00">
 Meeting
</MYTASK>

<MYTASK TIME = "11:00">
Lecture
</MYTASK>


<MYTASK TIME = "12:00">
 Lunch
</MYTASK>

And so on...

I can read it from res/xml folder using,

   Resources res = activity.getResources();
   XmlResourceParser xpp = res.getXml(R.xml.tv_editor_todo_list);
   xpp.next();
   int eventType = xpp.getEventType();
   while (eventType != XmlPullParser.END_DOCUMENT)
   {
    if(eventType == XmlPullParser.START_DOCUMENT)
    {
     stringBuffer.append("--- Start XML ---");

    }
    else if(eventType == XmlPullParser.START_TAG)
    {
     stringBuffer.append("\nSTART_TAG: "+xpp.getName());

     ...

My question how can I edit and update the XML file on the fly, for example, say change,

<MYTASK TIME = "10:00">
 Meeting
</MYTASK>

to

<YOURTASK TIME = "11:30">
 Reading
</YOURTASK>

And save the file back?

You cannot modify files that are stored in the res folder during runtime. You'll need to store your xml file either in your applications internal storage, or on the SDCard if you want to be able to modify and re-save.

Something like this will give you an optoutStream to your internal storage.

FileOutputStream fos = openFileOutput("yourfile.xml", Context.MODE_WORLD_READABLE);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM