黑马程序员技术交流社区
标题:
求:向xml中添加数据时怎么能不覆盖已有数据?
[打印本页]
作者:
李慧声
时间:
2013-6-18 17:57
标题:
求:向xml中添加数据时怎么能不覆盖已有数据?
本帖最后由 李慧声 于 2013-6-18 17:58 编辑
//求大神:动态添加xml数据,怎么样才能不覆盖xml已有的数据呢
复制代码
下面是写数据的过程,
public void saveInstall_Collection_Record_Info(List<Install_Collection_Info> mICInfos, Writer writer) throws Throwable{
XmlSerializer serializer = Xml.newSerializer();
serializer.setOutput(writer);
serializer.startDocument("UTF-8", true);
serializer.startTag(null, "result");
for(Install_Collection_Info mICInfo : mICInfos){
serializer.startTag(null, "record");
serializer.attribute(null, "appid", mICInfo.getAppid()+"");
serializer.startTag(null, "appname");
serializer.text(mICInfo.getAppname());
serializer.endTag(null, "appname");
serializer.startTag(null, "version");
serializer.text(mICInfo.getVersion());
serializer.endTag(null, "version");
serializer.startTag(null, "pkgName");
serializer.text(mICInfo.getPkgName());
serializer.endTag(null, "pkgName");
serializer.endTag(null, "record");
}
serializer.endTag(null, "result");
serializer.endDocument();
writer.flush();
writer.close();
}
复制代码
下面是调用上面的写数据。
public void saveInstall_Collection_Status(int appid,String appname,String version,String pkgName) throws Throwable{
List<Install_Collection_Info> mICInfos = new ArrayList<Install_Collection_Info>();
mICInfos.add(new Install_Collection_Info(appid, appname, version,pkgName));
File file = new File(MyApplication.getAppContext().getFilesDir(),"Install_Collection_Info.xml");
FileOutputStream outStream = null;
if(!(file.exists() && file.isFile()))//文件不存在,创建文件,否则,直接写文件
outStream = MyApplication.getAppContext().openFileOutput("Install_Collection_Info.xml",Context.MODE_PRIVATE);
OutputStreamWriter writer = new OutputStreamWriter(outStream, "UTF-8");
BufferedWriter bWriter = new BufferedWriter(writer);
saveInstall_Collection_Record_Info(mICInfos, bWriter);
}
复制代码
作者:
李慧声
时间:
2013-6-18 20:29
同志们别光总结啊 也解决一下问题撒
作者:
神之梦
时间:
2013-6-20 23:46
等待楼下大神。。。
作者:
Just_Only
时间:
2013-6-20 23:48
同等大神
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2