还记得上一篇我们讲到了用SharePreference来存储数据,那么究竟它是如何实现的呢,今天我们就来仔细看看其实现的细节,我们给它一个准确的名字,叫做XML序列化器(XmlSerializer)。

  • 不同于上面一篇的保存用户的登录名以及密码,这次我们保存设备中的信息,但是由于现在知识有限,我还不能够实现对设备中信息的读取,那么我就在程序中自己生成若干条信息,对这些生成的信息进行读取,并保存到位于SD卡的backup.xml文件中。在这里我是用两种方法对其进行存储并比较两种方法的优缺点,当然作为开发,我更建议使用待会讲到的第二种方法。

 先来看看我们需要做到什么样的效果:

图一


  • 第一种方法:使用StringBuffer,将所有的内容逐一追加到该字符流中。

 public void backUpSms1(View view){
StringBuffer sb=new StringBuffer();
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
sb.append("<smss>");
for (SmsInfo smsinfo: smsInfo) {
sb.append("<sms>");
sb.append("<address>");
sb.append(smsinfo.getAddress());
sb.append("</address>"); sb.append("<date>");
sb.append(smsinfo.getDate());
sb.append("</date>"); sb.append("<content>");
sb.append(smsinfo.getContent());
sb.append("</content>"); sb.append("<type>");
sb.append(smsinfo.getType());
sb.append("</type>");
sb.append("</sms>");
}
sb.append("</smss>");
try {
File file =new File(Environment.getExternalStorageDirectory(), "backUp1.xml");
FileOutputStream fos =new FileOutputStream(file);
fos.write(sb.toString().getBytes());
fos.close();
Toast.makeText( this, "备份成功",0).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText( this, "备份失败",0).show();
}
}

第一种方法相当简单,只是用了一个字符流,将所有的内容逐一追加就行了,可是当我们的短信中出现了一些比较特殊的字符,例如:"<"或者是">"那么在读取并写入到xml文件的时候就会出错,这个时候打开该xml文件的时候将会报错;另外当我们需要在sms标签中加入一些属性,如图一所示,那么这个时候仅仅利用字符流来写就会变得冗杂,而且逻辑关系就不严谨了,所以这个时候我们就需要用到第二种方法了。

  • 第二种方法(推荐):XML序列化器(使用XmlSerializer编辑xml文件)

 public void backUpSms2(View view) {
try {
//初始化序列号器,指定xml写入到哪个文件并指定写入的编码格式
XmlSerializer xmlSerializer =Xml.newSerializer();
File file =new File(Environment.getExternalStorageDirectory(), "backUp2.xml");
FileOutputStream fos=new FileOutputStream(file);
xmlSerializer.setOutput(fos,"utf-8"); xmlSerializer.startDocument("utf-8", true);//开始声明文件
xmlSerializer.startTag(null, "smss");//开始最外层标签
for (SmsInfo sms : smsInfo) {
xmlSerializer.startTag(null, "sms");//写入第一个标签
xmlSerializer.attribute(null, "id", sms.getId()+"");//第一个标签的属性 xmlSerializer.startTag(null, "address");//写入内一层的标签
xmlSerializer.text(sms.getAddress());
xmlSerializer.endTag(null, "address"); xmlSerializer.startTag(null, "date");//写入内一层的标签
xmlSerializer.text(sms.getDate()+"");
xmlSerializer.endTag(null, "date"); xmlSerializer.startTag(null, "content");//写入内一层的标签
xmlSerializer.text(sms.getContent());
xmlSerializer.endTag(null, "content"); xmlSerializer.startTag(null, "type");//写入内一层的标签
xmlSerializer.text(sms.getType()+"");
xmlSerializer.endTag(null, "type"); xmlSerializer.endTag(null, "sms");//结束标签 }
xmlSerializer.endTag(null, "smss");//结束最外层标签
xmlSerializer.endDocument();//结束声明文件
fos.close();//记得一定要关闭输出流
Toast.makeText( this, "备份成功",0).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText( this, "备份失败",0).show();
}
}

使用XmlSerializer的时候有几个步骤:1、首先需要找到输出流,即通过setOutput方法将输出流以及编码格式传入;2、接着需要声明文件以及结束声明,这是通过startDocument以及endDocument这两个方法来实现的;3、接着就可以通过startTag以及startTag方法来声明标签以及结束标签,要声明标签的内容的时候可以通过text方法,当然这个方法只能允许传入String类型,所以对于其他数据类型,需要先对其进行转换;4、最后一点就是上面讲到的——要在标签中添加属性,那就需要通过attribute方法声明id属性。

通过以上的讲解,我们可以得到以下的xml文件:

 <?xml version="1.0" encoding="UTF-8" standalone="true"?>

 -<smss>

 -<sms id="0">

 <address>135000000000</address>

 <date>1461845202224</date>

 <content>内容为:0</content>

 <type>1</type>

 </sms>

 -<sms id="1">

 <address>135000000001</address>

 <date>1461845202225</date>

 <content>内容为:1</content>

 <type>2</type>

 </sms>

 -<sms id="2">

 <address>135000000002</address>

 <date>1461845202225</date>

 <content>内容为:2</content>

 <type>1</type>

 </sms>

 -<sms id="3">

 <address>135000000003</address>

 <date>1461845202225</date>

 <content>内容为:3</content>

 <type>2</type>

 </sms>

 -<sms id="4">

 <address>135000000004</address>

 <date>1461845202225</date>

 <content>内容为:4</content>

 <type>2</type>

 </sms>

 -<sms id="5">

 <address>135000000005</address>

 <date>1461845202225</date>

 <content>内容为:5</content>

 <type>2</type>

 </sms>

 -<sms id="6">

 <address>135000000006</address>

 <date>1461845202225</date>

 <content>内容为:6</content>

 <type>2</type>

 </sms>

 -<sms id="7">

 <address>135000000007</address>

 <date>1461845202225</date>

 <content>内容为:7</content>

 <type>2</type>

 </sms>

 -<sms id="8">

 <address>135000000008</address>

 <date>1461845202225</date>

 <content>内容为:8</content>

 <type>1</type>

 </sms>

 -<sms id="9">

 <address>135000000009</address>

 <date>1461845202225</date>

 <content>内容为:9</content>

 <type>2</type>

 </sms>

 -<sms id="10">

 <address>135000000010</address>

 <date>1461845202225</date>

 <content>内容为:10</content>

 <type>1</type>

 </sms>

 -<sms id="11">

 <address>135000000011</address>

 <date>1461845202225</date>

 <content>内容为:11</content>

 <type>2</type>

 </sms>

 -<sms id="12">

 <address>135000000012</address>

 <date>1461845202225</date>

 <content>内容为:12</content>

 <type>2</type>

 </sms>

 -<sms id="13">

 <address>135000000013</address>

 <date>1461845202225</date>

 <content>内容为:13</content>

 <type>2</type>

 </sms>

 -<sms id="14">

 <address>135000000014</address>

 <date>1461845202225</date>

 <content>内容为:14</content>

 <type>2</type>

 </sms>

 -<sms id="15">

 <address>135000000015</address>

 <date>1461845202225</date>

 <content>内容为:15</content>

 <type>1</type>

 </sms>

 -<sms id="16">

 <address>135000000016</address>

 <date>1461845202225</date>

 <content>内容为:16</content>

 <type>1</type>

 </sms>

 -<sms id="17">

 <address>135000000017</address>

 <date>1461845202225</date>

 <content>内容为:17</content>

 <type>1</type>

 </sms>

 -<sms id="18">

 <address>135000000018</address>

 <date>1461845202225</date>

 <content>内容为:18</content>

 <type>2</type>

 </sms>

 -<sms id="19">

 <address>135000000019</address>

 <date>1461845202225</date>

 <content>内容为:19</content>

 <type>1</type>

 </sms>

 </smss>

2016-04-29

BOB

--开始忙碌的五一

SharePreference是如何实现的——序列化XML文件的更多相关文章

  1. 如何引用XML文件生成C#类

    目录 XSD File Generate Class File Simply. 1 Why use XSD file to create C# classes?... 2 How to convert ...

  2. Android 使用xml序列化器生成xml文件

    在<Android 生成xml文件>一文中使用流的形式写入xml格式文件,但是存在一定的问题,那就是在短信内容中不能出现<>之类的括号,本文使用xml序列化器来解决 xml序列 ...

  3. 使用XML序列化器生成XML文件和利用pull解析XML文件

    首先,指定XML格式,我指定的XML格式如下: <?xml version='1.0' encoding='utf-8' standalone='yes' ?> <message&g ...

  4. XML文件序列化和反序列化的相关内容

    问题缘由: XML反序列化出错,XML 文档(2, 2)中有错误,不应有 <configuration xmlns=''> 解决方法: 其实这个是很简单的,因为一般来说都是XML文档书写错 ...

  5. 使用XMl序列化器生成xml文件

    生成XML文件 创建几个虚拟的短信对象,存在list中 备份数据通常都是备份至sd卡 使用StringBuffer拼接字符串 把整个xml文件所有节点append到sb对象里 sb.append(&q ...

  6. WebAPI调用笔记 ASP.NET CORE 学习之自定义异常处理 MySQL数据库查询优化建议 .NET操作XML文件之泛型集合的序列化与反序列化 Asp.Net Core 轻松学-多线程之Task快速上手 Asp.Net Core 轻松学-多线程之Task(补充)

    WebAPI调用笔记   前言 即时通信项目中初次调用OA接口遇到了一些问题,因为本人从业后几乎一直做CS端项目,一个简单的WebAPI调用居然浪费了不少时间,特此记录. 接口描述 首先说明一下,基于 ...

  7. Android中XML文件的序列化生成与解析

    xml文件是非常常用的,在android中json和xml是非常常用的两种封装数据的形式,从服务器中获取数据也经常是这两种形式的,所以学会生成和解析xml和json是非常有用的,json相对来说是比较 ...

  8. XML文件的写,集合XML序列化(写)。XML文件的读,递归遍历

    XML文件:必须要有一个节点.检验xml文件,可以用浏览器打开,能打开表示对,否则错. 处理方法: DOM:XmlDocument文档对象模型 Sax(事件驱动,XmlReader) XmlSeria ...

  9. Android -- 创建XML文件对象及其序列化, pull解析XML文件

    1. 创建XML文件对象及其序列化 示例代码:(模拟以xml格式备份短信到SD卡) SmsInfo.java, bean对象 /** * 短信的业务bean * @author Administrat ...

随机推荐

  1. vs2010设置

    解决方案管理器文件自动定位:工具--选项--项目和解决方案--常规--在解决方案资源管理器中跟踪活动项(前打勾). VAssistX拼写错误的下划波浪线去掉:在VAssistX菜单栏->Visu ...

  2. 爬虫技术 -- 进阶学习(八)模拟简单浏览器(附c#代码)

    由于最近在做毕业设计,需要用到一些简单的浏览器功能,于是学习了一下,顺便写篇博客~~大牛请勿喷,菜鸟练练手~ 实现界面如下:(简单朴素版@_@||) button_go实现如下: private vo ...

  3. 浅析.NET泛型

    泛型是.NET Framework 2.0最强大的功能,通过泛型可以定义类型安全的数据结构,而没有必要使用实际的数据类型,这将显著提高性能并得到更高质量的代码.在.NET Framework 2.0之 ...

  4. Hadoop入门进阶课程9--Mahout介绍、安装与应用案例

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,博主为石山园,博客地址为 http://www.cnblogs.com/shishanyuan  ...

  5. 关于python中赋值、浅拷贝、深拷贝之间区别的深入分析

    当重新学习了计算机基础课程<数据结构和算法分析>后再来看这篇自己以前写的博文,发现错误百出.python内置数据类型之所以会有这些特性,归根结底是它采用的是传递内存地址的方式,而不是传递真 ...

  6. awk引用外部变量及调用系统命令方法

    目标:想用awk与scp命令批量传送文件 前提:先搭好主机间的免密登陆环境(参考:http://www.cnblogs.com/tankaixiong/p/4172942.html) 实现脚本方法: ...

  7. Entity FrameWork 延迟加载本质(二)

    1.对于外键实体而言,EF会在用到这个外键属性的时候,才会去查对应的表.这就是按需加载了... 2.按需加载的缺点:每次调用外键实体的时候,都会去查询数据库(EF有小优化:相同的外键实体只查一次) I ...

  8. WinForm界面开发之布局控件"WeifenLuo.WinFormsUI.Docking"的使用

    WinForm界面开发之布局控件"WeifenLuo.WinFormsUI.Docking"的使用 转自:http://www.cnblogs.com/wuhuacong/arch ...

  9. 重构第9天:提取接口(Extract Interface)

    理解:提取接口的意思是,多于一个类共同使用某个类中的方法或属性,那么我们可以把这些方法和属性提出来,作为一个单独的接口.这样的好处是解除代码间的依赖,降低耦合性. 详解: 先看重构前的代码: publ ...

  10. Azure开发者任务之一:解决Azure Storage Emulator初始化失败

    初学Windows Azure: 我打算开始学习Windows Azure.我安装了Azure SDK,然后在“Cloud”标签下选择Windows Azure模板,创建了一个项目,然后又创建了一个W ...