信息類
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();
}
復制代碼