Android 產生xml檔案

來源:互聯網
上載者:User

標籤:

產生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>

建立Sms類

package com.wuyudong.createxml.domain;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;    }}

整幾個虛擬簡訊對象,存在list中,備份資料通常都是備份至sd卡

使用StringBuffer拼接字串,* 把整個xml檔案所有節點append到sb對象裡

sb.append("<?xml version=‘1.0‘ encoding=‘utf-8‘ standalone=‘yes‘ ?>");
//添加smss的開始節點
sb.append("<smss>");
.......
* 把sb寫到輸出資料流中

fos.write(sb.toString().getBytes());

完整代碼如下:

package com.wuyudong.createxml;import java.io.File;import java.io.FileOutputStream;import java.util.ArrayList;import java.util.List;import com.wuyudong.createxml.domain.Sms;import android.os.Bundle;import android.os.Environment;import android.app.Activity;import android.view.Menu;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(),                "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();        }    }}

Android 產生xml檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.