Android -- 写xml到SD卡中

信息类                                                                                         

private String body;
private String number;
private int type;
private long id;

四个变量,然后有分别set和get。构造函数分别初始化这四个变量。

生成xml标签函数                                                                          

public static void backUpXml(File file, List<XmlInfo> smsInfos) throws Exception
{
//xml文件的序列号器 帮助生成一个xml文件
FileOutputStream fos = new FileOutputStream(file);
//获取到xml的序列号
XmlSerializer serializer = Xml.newSerializer();
//序列化初始化
serializer.setOutput(fos, "utf-8");
//创建xml
serializer.startDocument("utf-8", true);
serializer.startTag(null, "smss"); for(XmlInfo info : smsInfos)
{
serializer.startTag(null, "sms");
serializer.attribute(null, "id", String.valueOf(info.getId())); serializer.startTag(null, "body");
serializer.attribute(null, "body", info.getBody());
serializer.endTag(null, "body"); serializer.startTag(null, "number");
serializer.text(info.getNumber());
serializer.endTag(null, "number"); serializer.endTag(null, "sms");
} serializer.endTag(null, "smss");
serializer.endDocument(); fos.flush();
fos.close(); }

onCreate中进行处理                                                                

List<XmlInfo> xmlinfo = new ArrayList<XmlInfo>();

        XmlInfo xml1 = new XmlInfo("aaaa","12345678912",1,1);
XmlInfo xml2 = new XmlInfo("bbbb","21987654321",1,2);
XmlInfo xml3 = new XmlInfo("cccc","12345678912",1,3); xmlinfo.add(xml1);
xmlinfo.add(xml2);
xmlinfo.add(xml3); File file = new File(Environment.getExternalStorageDirectory(),"back.xml");
//File file = new File(MainActivity.this.getFilesDir(),"back.xml");
try {
XmlManager.backUpXml(file, xmlinfo);
Toast.makeText(this, "备份成功", 0).show();
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
Toast.makeText(this, "备份失败", 0).show();
}

我是天王盖地虎的分割线                                                                 

源代码:http://pan.baidu.com/s/1dD1Qx01

Xml普通解析.zip

转载请注明出处:http://www.cnblogs.com/yydcdut

上一篇:android之写文件到sd卡


下一篇:cygwin下烧写文件到sd卡中