Android 生成xml文件

Android解析xml文件

两种方式生成:
1、通过StringBuffer直接以追加字符串的方式生成XML文件
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(),
                "backup.xml");
        try {
            FileOutputStream fos = new FileOutputStream(file);
            StringBuffer sb = new StringBuffer();
            // 添加xml头
            sb.append("<?xml version='1.0' encoding='utf-8' standalone='yes' ?>");
            // 添加根节点
            sb.append("<message>");
            // 每一条短信添加一个sms节点
            for (Sms sms : message) {
                sb.append("<sms>");
                sb.append("<body>");
                sb.append(sms.getBody());
                sb.append("</body>");
                sb.append("<date>");
                sb.append(sms.getDate());
                sb.append("</date>");
                sb.append("<address>");
                sb.append(sms.getAddress());
                sb.append("</address>");
                sb.append("<type>");
                sb.append(sms.getType());
                sb.append("</type>");
                sb.append("</sms>");
            }
            sb.append("</message>");
            fos.write(sb.toString().getBytes());
            fos.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
2、利用XmlSerializer类来生成XML文件
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.xml");
        try {
            FileOutputStream fos = new FileOutputStream(file);
            // 获取xml序列化器
            XmlSerializer xs = Xml.newSerializer();
            xs.setOutput(fos, "utf-8");
            //生成xml头
            xs.startDocument("utf-8", true);
            //添加xml根节点
            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");
            //生成xml头
            xs.endDocument();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
最终生成的xml格式:
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
<message>  
  <sms>
    <body>第0条短信</body>
    <date>1465041254178</date>
    <address>000</address>
    <type>1</type>
  </sms>  
  <sms>
    <body>第1条短信</body>
    <date>1465041254179</date>
    <address>111</address>
    <type>1</type>
  </sms>  
  <sms>
    <body>第2条短信</body>
    <date>1465041254179</date>
    <address>222</address>
    <type>1</type>
  </sms>
</message>
数据载体:
public class Sms {

    private String body;
    private String date;
    private String type;
    private String address;

    public Sms(String body, String date, String type, String address) {
        super();
        this.body = body;
        this.date = date;
        this.type = type;
        this.address = address;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

}
    原文作者:Hans在路上
    原文地址: https://www.jianshu.com/p/cad4a2f7f9d0
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞