<span style="font-size:18px;">MainActivity 代码:</span>
<span style="font-size:18px;">package com.itheima.createxml; import java.io.File; import java.io.FileFilter; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; import org.xmlpull.v1.XmlSerializer; import com.itheima.createxml.domain.Sms; import android.os.Bundle; import android.app.Activity; import android.util.Xml; import android.view.Menu; import android.view.View; public class MainActivity extends Activity { List<Sms> smsList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); smsList = new ArrayList<Sms>(); for (int i = 0; i < 10; i++) { Sms sms = new Sms("你好" + i, System.currentTimeMillis(), 1, "138438"); smsList.add(sms); } } public void click(View v){ // StringBuffer sb = new StringBuffer(); // sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); // sb.append("<smss>"); // // for (Sms sms : smsList) { // sb.append("<sms>"); // // sb.append("<body>"); // sb.append(sms.getBody()+"<body>"); // sb.append("</body>"); // // sb.append("<date>"); // sb.append(sms.getDate()); // sb.append("</date>"); // // sb.append("<type>"); // sb.append(sms.getType()); // sb.append("</type>"); // // sb.append("<address>"); // sb.append(sms.getAddress()); // sb.append("</address>"); // // sb.append("</sms>"); // } // // sb.append("</smss>"); // // File file = new File("sdcard/sms.xml"); // try { // FileOutputStream fos = new FileOutputStream(file); // fos.write(sb.toString().getBytes()); // } catch (Exception e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } //获取xml序列化器 XmlSerializer xs = Xml.newSerializer(); File file = new File("sdcard/sms2.xml"); FileOutputStream fos; try { fos = new FileOutputStream(file); //初始化 //xml文件中什么编码生成 xs.setOutput(fos, "utf-8"); //开始生成xml文件 //生成头结点 xs.startDocument("utf-8", true); //生成开始标签 xs.startTag(null, "smss"); for (Sms sms : smsList) { xs.startTag(null, "sms"); xs.startTag(null, "body"); xs.text(sms.getBody() + "<body>"); xs.endTag(null, "body"); xs.startTag(null, "type"); xs.text(sms.getType() + ""); xs.endTag(null, "type"); 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.endTag(null, "sms"); } //生成结束标签 xs.endTag(null, "smss"); //告知序列化器生成xml结束 xs.endDocument(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } </span>
<span style="font-size:18px;">//////////////////////////////Sms.java 代码//////////////////////////</span>
<span style="font-size:18px;">package com.itheima.createxml.domain; public class Sms { <span style="white-space:pre"> </span>private String body; <span style="white-space:pre"> </span>private long date; <span style="white-space:pre"> </span>private int type; <span style="white-space:pre"> </span>private String address; <span style="white-space:pre"> </span>public Sms(String body, long date, int type, String address) { <span style="white-space:pre"> </span>super(); <span style="white-space:pre"> </span>this.body = body; <span style="white-space:pre"> </span>this.date = date; <span style="white-space:pre"> </span>this.type = type; <span style="white-space:pre"> </span>this.address = address; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public String getBody() { <span style="white-space:pre"> </span>return body; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public void setBody(String body) { <span style="white-space:pre"> </span>this.body = body; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public long getDate() { <span style="white-space:pre"> </span>return date; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public void setDate(long date) { <span style="white-space:pre"> </span>this.date = date; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public int getType() { <span style="white-space:pre"> </span>return type; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public void setType(int type) { <span style="white-space:pre"> </span>this.type = type; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public String getAddress() { <span style="white-space:pre"> </span>return address; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public void setAddress(String address) { <span style="white-space:pre"> </span>this.address = address; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span> <span style="white-space:pre"> </span> } </span>