[英]How to edit XML in Android and save?
I have an XML file, as 我有一个XML文件,如
<?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, 我可以从res / xml文件夹中读取它,
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, 我的问题是如何动态编辑和更新XML文件,例如,更改,
<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. 您无法在运行时修改存储在res文件夹中的文件。 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. 如果您希望能够修改和重新保存,则需要将xml文件存储在应用程序内部存储中或SDCard中。
Something like this will give you an optoutStream to your internal storage. 这样的东西会给你一个optoutStream到你的内部存储。
FileOutputStream fos = openFileOutput("yourfile.xml", Context.MODE_WORLD_READABLE);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.