你的位置:首页 > 操作系统

[操作系统]Android 使用xml序列化器生成xml文件


在《Android 生成

* 给序列化器设置输出流

File file = new File(Environment.getExternalStorageDirectory(), "backupsms. FileOutputStream fos = new FileOutputStream(file);
//给序列化器指定好输出流
xs.setOutput(fos, "utf-8");
* 开始生成

xs.startDocument("utf-8", true);
xs.startTag(null, "smss");
......

代码如下:

package com.wuyudong.createimport java.io.File;import java.io.FileOutputStream;import java.util.ArrayList;import java.util.List;import org.import com.wuyudong.createimport com.wuyudong.createimport android.os.Bundle;import android.os.Environment;import android.app.Activity;import android.util.import android.view.View;public class MainActivity extends Activity {  List<Sms> message;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    // 创建10条虚拟短信    message = new ArrayList<Sms>();    for (int i = 0; i < 10; i++) {      Sms sms = new Sms("第" + i + "条短信", System.currentTimeMillis() + "",          "1", "" + i + i + i);      message.add(sms);    }  }  public void click(View v) {    File file = new File(Environment.getExternalStorageDirectory(),        "backup1.);    try {      FileOutputStream fos = new FileOutputStream(file);      // 获取       "utf-8");      //生成      xs.startDocument("utf-8", true);      //添加      xs.startTag(null, "message");      for (Sms sms : message) {        xs.startTag(null, "sms");        xs.startTag(null, "body");        xs.text(sms.getBody());        xs.endTag(null, "body");        xs.startTag(null, "date");        xs.text(sms.getDate());        xs.endTag(null, "date");        xs.startTag(null, "address");        xs.text(sms.getAddress());        xs.endTag(null, "address");        xs.startTag(null, "type");        xs.text(sms.getType());        xs.endTag(null, "type");        xs.endTag(null, "sms");      }      xs.endTag(null, "message");      //生成      xs.endDocument();    } catch (Exception e) {      // TODO Auto-generated catch block      e.printStackTrace();    }  }}