一.andoirdAPI提供了xml生成和解析的API: XmlSerializer xs = Xml.newSerializer();和XmlPullParser xmlPullParser = Xml.newPullParser();
1.使用xml序列化器(XmlSerializer)生成xml文件
@1获取序列化器实例
XmlSerializer xs = Xml.newSerializer();
@2设置序列化器参数
File file = new File(Environment.getExternalStorageDirectory().getPath(),"com.xml");
FileOutputStream fos = new FileOutputStream(file);
xs.setOutput(fos,"utf-8");
@3写xml文档开头
xs.startDocument("utf-8",true);
@4写根节点
xs.startTag(null,"smss");
xs.endTag(null,"smss");
@5循环写其他节点
for(Sms sms:list){
xs.startTag(null,"sms");
xs.startTag(null,"addr");
xs.text(sms.getAddr());
xs.endTag(null,"addr");
xs.startTag(null,"content");
xs.text(sms.getContent());
xs.endTag(null,"content");
xs.startTag(null,"date");
xs.text(sms.getDate());
xs.endTag(null,"date");
xs.endTag(null,"sms");
}
@6写文档末尾
xs.endDocument();
运行结果:/mnt/sdcard/下生成了com.xml文件
![](https://i-blog.csdnimg.cn/blog_migrate/197935404e9b0f9a38dec13f87f3e042.png)
文档内容:文件内容如下与写入相符合
![](https://i-blog.csdnimg.cn/blog_migrate/227d1d6fa4a02e14e3a7b950ca3ead4d.png)