XML的序列化(Serializer)

步骤:

//1获取XmlSerializer 类的实例  通过Xml这个工具类去获取
XmlSerializer xmlSerializer = Xml.newSerializer();
try {
//2.设置 xmlserializer序列化器参数
File file = new File(Environment.getExternalStorageDirectory().getPath(), "smsInfo.xml");
FileOutputStream fos = new FileOutputStream(file);
xmlSerializer.setOutput(fos, "utf-8");
//3开始写xml文档开头,结尾也写上
xmlSerializer.startDocument("utf-8",true);
//4.写xml的根节点开头 就是namespace命名空间 结尾也要一起写
xmlSerializer.startTag(null,"smss");
//5.循环来写sms节点
for (SMS sms:smsLists) {
//6正式开始写内容 adddress节点
xmlSerializer.startTag(null,"address");
xmlSerializer.text(sms.getAddress());
xmlSerializer.endTag(null,"address");
//7正式开始写内容 body节点
xmlSerializer.startTag(null,"body");
xmlSerializer.text(sms.getBody());
xmlSerializer.endTag(null,"body");
//8正式开始写内容 date节点
xmlSerializer.startTag(null,"date");
xmlSerializer.text(sms.getDate());
xmlSerializer.endTag(null,"date");

}
xmlSerializer.endTag(null,"smss");
//文档结尾
xmlSerializer.endDocument();
//关闭流
fos.close();

} catch (Exception e) {
e.printStackTrace();
}


}
posted @ 2017-09-23 16:15  LiangFuCheng  阅读(485)  评论(0编辑  收藏  举报