本帖最后由 李慧声 于 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);
- }
复制代码 |