xml文件的序列化 →创建xml文件

来源:互联网 发布:js修改classname 编辑:程序博客网 时间:2024/05/22 06:06

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// bodyserializer.startTag(null, "body");serializer.text(info.getBody());serializer.endTag(null, "body");// addressserializer.startTag(null, "address");serializer.text(info.getAddress());serializer.endTag(null, "address");// typeserializer.startTag(null, "type");serializer.text(info.getType() + "");serializer.endTag(null, "type");// dateserializer.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 blocke.printStackTrace();Toast.makeText(this, "备份失败", Toast.LENGTH_SHORT).show();}}


0 0
原创粉丝点击