- xml序列化
- 实现思路 通过程序生成一个xml文件来备份手机短信. 先获取手机短信的内容 —>通过xml备份.
- StringBuffer 代码如下
public void click(View view) { StringBuffer sb = new StringBuffer(); sb.append(" "); sb.append(""); for (SMSbean list : mLists) { sb.append(" "); sb.append(" "); sb.append(list.getAddress()); sb.append(""); sb.append(""); sb.append(list.getBody()); sb.append(""); sb.append(" "); } sb.append(""); sb.append(list.getDate()); sb.append(" "); sb.append(""); 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(); } }