<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 {
private String body;
private long date;
private int type;
private String address;
public Sms(String body, long date, int 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 long getDate() {
return date;
}
public void setDate(long date) {
this.date = date;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}