xm格式如下:
创建Button监听器
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/bt" android:onClick="backSms2" android:text="第二种方式短信的备份" />
在model层中增加变量id
private int id; // 短信的id
/** * 第二种方式生成xml文件 * * @param view */ public void backSms2(View view) { try { XmlSerializer serializer = Xml.newSerializer(); // xml的序列化器 File file = new File(Environment.getExternalStorageDirectory(),"backup2.xml"); FileOutputStream fos = new FileOutputStream(file); serializer.setOutput(fos,"utf-8"); // 初始化序列号器,指定xml数据写入到哪个文件,并且指定文件的编码方式 serializer.startDocument("utf-8",true); // 开头 serializer.startTag(null,"smss");// 开始结点 for (SmsInfo info : smsInfos) { serializer.startTag(null,"sms"); // sms开始 serializer.attribute(null,"id",info.getId() + ""); // id // body serializer.startTag(null,"body"); serializer.text(info.getBody()); serializer.endTag(null,"body"); // address serializer.startTag(null,"address"); serializer.text(info.getAddress()); serializer.endTag(null,"address"); // type serializer.startTag(null,"type"); serializer.text(info.getType() + ""); serializer.endTag(null,"type"); // date serializer.startTag(null,"date"); serializer.text(info.getDate() + ""); serializer.endTag(null,"date"); serializer.endTag(null,"sms"); // sms结束 } serializer.endTag(null,"smss"); // 结束结点 serializer.endDocument(); // 结尾 fos.close(); Toast.makeText(this,"备份成功",Toast.LENGTH_SHORT).show(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); Toast.makeText(this,"备份失败",Toast.LENGTH_SHORT).show(); } }原文链接:https://www.f2er.com/xml/298298.html