小技巧:
eclipse 方式生成get set方法 右键 Source -->Generate Getters android Setters
eclipsq 生成构造方法 右键 Source -->Generate Constructors using Fileds
示例代码:
List<Smsinfo> smsList;
smsList = new ArrayList<Smsinfo>();
Random random = new Random();
for (int i = 0; i < 10; i++) {
smsList.add(new Smsinfo(i, "发送短信" + i, "1580134859" + i, random
.nextInt(2)));
}
try {
XmlSerializer xmlSerializer = Xml.newSerializer();
File smsFile = new File(Environment.getExternalStorageDirectory(),
"sms.txt");
FileOutputStream fileOutputStream = new FileOutputStream(smsFile);
xmlSerializer.setOutput(fileOutputStream, "utf-8");
xmlSerializer.startDocument("utf-8", true);// 布尔代表是否是独立文件
xmlSerializer.startTag(null, "smss");
for (Smsinfo smsinfo : smsList) {
xmlSerializer.startTag(null, "sms");
xmlSerializer.attribute(null, "id", smsinfo.getId() + "");
xmlSerializer.startTag(null, "date");
xmlSerializer.text(smsinfo.getDate());
xmlSerializer.endTag(null, "date");
xmlSerializer.startTag(null, "address");
xmlSerializer.text(smsinfo.getAddress());
xmlSerializer.endTag(null, "address");
xmlSerializer.startTag(null, "type");
xmlSerializer.text(smsinfo.getType() + "");
xmlSerializer.endTag(null, "type");
xmlSerializer.endTag(null, "sms");
}
xmlSerializer.endTag(null, "smss");
xmlSerializer.endDocument();
fileOutputStream.close();
} catch (Exception e) {
Toast.makeText(MainActivity.this, "失败", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}