星空网 > 软件开发 > 操作系统

Android 生成xml文件

生成

<??>
<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.createpublic 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拼接字符串,* 把整个

sb.append("<? //添加smss的开始节点
sb.append("<smss>");
.......
* 把sb写到输出流中

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

完整代码如下:

package com.wuyudong.createimport java.io.File;import java.io.FileOutputStream;import java.util.ArrayList;import java.util.List;import com.wuyudong.createimport 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.);    try {      FileOutputStream fos = new FileOutputStream(file);      StringBuffer sb = new StringBuffer();      // 添加      sb.append("<?);      // 添加根节点      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文件

关键词:Android

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

菲律宾运价:https://www.goluckyvip.com/tag/96341.html
邮寄去菲律宾:https://www.goluckyvip.com/tag/96342.html
从福建寄到菲律宾:https://www.goluckyvip.com/tag/96343.html
菲律宾目的港:https://www.goluckyvip.com/tag/96344.html
菲律宾cn2:https://www.goluckyvip.com/tag/96345.html
寄菲律宾:https://www.goluckyvip.com/tag/96346.html
昆明旅游索道公司 昆明旅游索道公司有哪些:https://www.vstour.cn/a/406243.html
太仓美食网(探寻太仓最美味的美食):https://www.vstour.cn/a/406244.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流