标签:
一、使用append进行xml生成
Message类属性:private String body;
private String date;
private String address;
private String type;
public class MainActivity extends Activity {
List<Message> smsList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//虚拟10条短信数据
smsList = new ArrayList<Message>();
for(int i = 0; i < 10; i++){
Message sms = new Message("小志好棒" + i, System.currentTimeMillis() + "", "601"+i, "1");
smsList.add(sms);
}
}
public void click(View v){
//在内存中把xml备份短信的格式拼接出来
StringBuffer sb = new StringBuffer();
sb.append("<?xml version=‘1.0‘ encoding=‘utf-8‘ standalone=‘yes‘ ?>");
sb.append("<messages>");
for (Message sms : smsList) {
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("<type>");
sb.append(sms.getType());
sb.append("</type>");
sb.append("<address>");
sb.append(sms.getAddress());
sb.append("</address>");
sb.append("</sms>");
}
sb.append("</messages>");
File file = new File("sdcard/sms.xml");
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(sb.toString().getBytes());
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
二、使用xml序列化器进行xml文件生成
Message类属性:private String body;
private String date;
private String address;
private String type;
public class MainActivity extends Activity {
List<Message> smsList; //定义集合
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 虚拟10条数据
smsList = new ArrayList<Message>();
for (int i = 0; i < 10; i++) {
Message sms = new Message("小志好棒" + i, System.currentTimeMillis()
+ "", "601" + i, "1");
smsList.add(sms);
}
}
public void click(View v){
//使用xml序列化器生成xml文件
//1.拿到序列化器对象
XmlSerializer xs = Xml.newSerializer();
//2.初始化
File file = new File("sdcard/sms2.xml");
try {
FileOutputStream fos = new FileOutputStream(file);
//enconding:指定用什么编码生成xml文件
xs.setOutput(fos, "utf-8");
//3.开始生成xml文件
//enconding:指定头结点中的enconding属性的值
xs.startDocument("utf-8", true);
xs.startTag(null, "message");
for (Message sms : smsList) {
xs.startTag(null, "sms");
xs.startTag(null, "body");
xs.text(sms.getBody() + "<body>");
xs.endTag(null, "body");
xs.startTag(null, "date");
xs.text(sms.getDate());
xs.endTag(null, "date");
xs.startTag(null, "type");
xs.text(sms.getType());
xs.endTag(null, "type");
xs.startTag(null, "address");
xs.text(sms.getAddress());
xs.endTag(null, "address");
xs.endTag(null, "sms");
}
xs.endTag(null, "message");
//告诉序列化器,文件生成完毕
xs.endDocument();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
三、使用PULL解析文件解析xml文件
City的属性: private String name;
private String temp;
private String pm
public class MainActivity extends Activity {
List<City> cityList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View v){
//获取到src文件夹下的资源文件
InputStream is = getClassLoader().getResourceAsStream("weather.xml");
//拿到pull解析器对象
XmlPullParser xp = Xml.newPullParser();
//初始化
try {
xp.setInput(is, "gbk");
//获取当前节点的事件类型,通过事件类型的判断,我们可以知道当前节点是什么节点,从而确定我们应该做什么操作
int type = xp.getEventType();
City city = null;
while(type != XmlPullParser.END_DOCUMENT){
//根据节点的类型,要做不同的操作
switch (type) {
case XmlPullParser.START_TAG:
// 获取当前节点的名字
if("weather".equals(xp.getName())){
//创建city集合对象,用于存放city的javabean
cityList = new ArrayList<City>();
}
else if("city".equals(xp.getName())){
//创建city的javabean对象
city = new City();
}
else if("name".equals(xp.getName())){
// 获取当前节点的下一个节点的文本
String name = xp.nextText();
city.setName(name);
}
else if("temp".equals(xp.getName())){
// 获取当前节点的下一个节点的文本
String temp = xp.nextText();
city.setTemp(temp);
}
else if("pm".equals(xp.getName())){
// 获取当前节点的下一个节点的文本
String pm = xp.nextText();
city.setPm(pm);
}
break;
case XmlPullParser.END_TAG:
if("city".equals(xp.getName())){
//把city的javabean放入集合中
cityList.add(city);
}
break;
}
//把指针移动到下一个节点,并返回该节点的事件类型
type = xp.next();
}
for (City c : cityList) {
System.out.println(c.toString());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
标签:
原文地址:http://www.cnblogs.com/inunou/p/5550635.html