如何在Android中附加XML文件? [英] How to append an XML file in Android?

查看:191
本文介绍了如何在Android中附加XML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在向SD卡写一个XML文件,我需要能够打开该XML文件并将数据附加到它。我怎么能做到这一点?例如,我的XML文件是:

I'm writing an XML file to the sd card, and I need to be able to open that XML file and append data to it. How can I accomplish this? For example, my XML file is:

<items>
    <item attr="value">data</item>
    <item attr="value2">data 2</item>
</items>

我稍后需要打开这个XML文件并向其添加一个新项目:

I later need to open this XML file and append a new item to it:

<item attr="value3">data 3</item>

我该怎么做?

推荐答案

您可以使用读取文件的sax解析器,默认输出所有元素,当它找到关闭{}时,首先插入新项目,然后插入结束标记。

You can use a sax parser that reads the file, outputs all elements by default and when it finds the closing {}, first insert the new item, then the closing tag.

Sax解析器在Android中默认可用。

Sax parsers are available by default in Android.

关于如何处理Sax的一个例子可以在这里找到

One example on how to deal with Sax at all can be found here.

这篇关于如何在Android中附加XML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆