Xml序列化
Posted nangongyibin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Xml序列化相关的知识,希望对你有一定的参考价值。
- xml序列化
- 实现思路 通过程序生成一个xml文件来备份手机短信. 先获取手机短信的内容 —>通过xml备份.
- StringBuffer 代码如下
public void click(View view) { StringBuffer sb = new StringBuffer(); sb.append("<?xml version="1.0" encoding="UTF-8"?>"); sb.append("<SMSS>"); for (SMSbean list : mLists) { sb.append("<SMS>"); sb.append("<address>"); sb.append(list.getAddress()); sb.append("</address>"); sb.append("<body>"); sb.append(list.getBody()); sb.append("</body>"); sb.append("<date>"); sb.append(list.getDate()); sb.append("</date>"); sb.append("</SMS>"); } sb.append("<SMSS>"); try { String s = sb.toString(); // Log.d(TAG, "click: "+s); String path = Environment.getExternalStorageDirectory().getAbsolutePath(); Log.d(TAG, "click: " + path); File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "Config.xml"); FileOutputStream fos = new FileOutputStream(file); fos.write(s.getBytes()); fos.close(); Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show(); } catch (IOException e) { e.printStackTrace(); Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show(); } }
-
- 通过官方提供序列化类实现xml序列化(通过程序生成一个xml)
public void click1(View view) { try { XmlSerializer serializer = Xml.newSerializer(); File file = new File(getFilesDir().getPath(), "ConfigCopy.xml"); String path = getFilesDir().getPath(); Log.d(TAG, "click1: " + path); FileOutputStream fos = new FileOutputStream(file); serializer.setOutput(fos, "utf-8"); serializer.startDocument("utf-8", true); serializer.startTag(null, "smss"); for (int i = 0; i < mLists.size(); i++) { serializer.startTag(null, "sms"); serializer.startTag(null, "address"); serializer.text(mLists.get(i).getAddress()); serializer.endTag(null, "address"); serializer.startTag(null, "body"); serializer.text(mLists.get(i).getBody()); serializer.endTag(null, "body"); serializer.startTag(null, "date"); serializer.text(mLists.get(i).getDate()); serializer.endTag(null, "date"); serializer.endTag(null, "sms"); } serializer.endTag(null, "smss"); serializer.endDocument(); fos.close(); Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show(); } catch (IOException e) { e.printStackTrace(); Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show(); } }
以上是关于Xml序列化的主要内容,如果未能解决你的问题,请参考以下文章