/**
* 第一种生成xml方式
* @param v
*/
public void click1(View v) {
StringBuffer sb = new StringBuffer();
sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sb.append("<smss>");
for (Sms sms : smsLists) {
sb.append("<sms>");
sb.append("<sender>");
sb.append(sms.getSender());
sb.append("<file:///C:\Users\ADMINI~1\AppData\Local\Temp\V7(XMWRN]{G8~CI}BCCR3QC.gifnder>");
sb.append("<content>");
sb.append(sms.getContent());
sb.append("</content>");
sb.append("<date>");
sb.append(sms.getDate());
sb.append("</date>");
sb.append("</sms>");
}
sb.append("</smss>");
File file = new File(Environment.getExternalStorageDirectory(),"smsbackup.xml");
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(sb.toString().getBytes());
fos.close();
Toast.makeText(getApplicationContext(), "sucess", 0).show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 第二种生成xml方式
* @param v
*/
public void click2(View v) throws Exception{
// 1 创建一个序列化器对象
XmlSerializer serializer = Xml.newSerializer();
//2设置 serializer 的参数
File file = new File(Environment.getExternalStorageDirectory(),"smsbackup11.xml");
FileOutputStream fos = new FileOutputStream(file);
serializer.setOutput(fos, "utf-8");
// 3 开始写 xml
serializer.startDocument("utf-8", true);
serializer.startTag(null, "smss");
for (Sms sms : smsLists) {
serializer.startTag(null, "sms");
serializer.startTag(null, "sender");
serializer.text(sms.getSender());
serializer.endTag(null, "sender");
serializer.startTag(null, "content");
serializer.text(sms.getContent());
serializer.endTag(null, "content");
serializer.startTag(null, "date");
serializer.text(sms.getDate());
serializer.endTag(null, "date");
serializer.endTag(null, "sms");
}
serializer.endTag(null, "smss");
serializer.endDocument();
fos.close();
}
注意:需要一个sms的javaBean做数据提供 。才可以生成
|