简体   繁体   English

如何在Android中编辑XML并保存?

[英]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.

相关问题 如何在Android Studio中的xml中编辑多个元素 - How to edit in multiple elements in an xml in android studio 如何使用Android将对象保存在xml文件中? - How to save an object in an xml file using Android? 如何在内部存储Android中保存XML文件 - How to save an XML file in Internal Storage Android 如何将ListView保存到Android中的xml文件中? - How to save a ListView into a xml file in Android? 如何在 android 中的 xml 中的 append 标签? 以及如何保存 xml 文件? - how to append tags in xml in android? and how save that xml file? 如何使用富文本格式保存Android编辑文本内容 - How to save Android edit text content with rich text 如何在 Android 应用程序中保存上次输入的编辑文本值? - how to save last entered edit text value in Android App? Android:可以从.xml布局中保存一些内容(即“编辑文本”) - Android: Would it be possible to save some of the content from your .xml layout (i.e. Edit Text) 如何将数据写入 xml 文件并将 xml 保存在 android 应用程序中 - how to write data to xml file and save that xml in android application 如何将图像添加到编辑文本字段中以及如何将其作为整个编辑文本字段另存为Android中的图像 - How to add the images into the edit text field and how to save it as the whole edit text field as image in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM