标签:xml xmlpullparser
一丶XML的创建
手动创建(保存到sd卡)
String status = Environment.getExternalStorageState();
if (status.equals(Environment.MEDIA_MOUNTED)) {
// 获取sd卡的路径
String path = Environment.getExternalStorageDirectory()
+ "/sms.xml";
// 创建file对象
File file = new File(path);
// 创建输出流对象
try {
FileOutputStream fos = new FileOutputStream(file);
StringBuilder sb = new StringBuilder();
sb.append("<?xml version=‘1.0‘ encoding=‘UTF-8‘ ?>");
sb.append("<Smses>");
for (int i = 0; i < list.size(); i++) {
Sms sms = list.get(i);
sb.append("<sms>");
sb.append("<address>" + sms.getAddress() + "</address>");
sb.append("<body>" + sms.getBody() + "</body>");
sb.append("<date>" + sms.getDate() + "</date>");
sb.append("</sms>");
}
sb.append("</Smses>");
fos.write(sb.toString().getBytes());
fos.close();
Toast.makeText(this, "创建xml文件成功", 0).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "创建xml文件失败", 0).show();
}
}else{
Toast.makeText(this, "sd卡不可用", 0).show();
}
调用系统的api创建
String status = Environment.getExternalStorageState();
if (status.equals(Environment.MEDIA_MOUNTED)) {
// 获取sd卡的路径
String path = Environment.getExternalStorageDirectory()
+ "/sms.xml";
//创建xml文件的生成器
try {
XmlSerializer serialiaer = Xml.newSerializer() ;
FileOutputStream os = new FileOutputStream(path) ;
serialiaer.setOutput(os, "UTF-8") ;
serialiaer.startDocument("UTF-8", true) ;
serialiaer.startTag(null, "Smses") ;
for (int i = 0; i < list.size(); i++) {
serialiaer.startTag(null, "sms") ;
serialiaer.startTag(null, "address") ;
serialiaer.text(list.get(i).getAddress()) ;
serialiaer.endTag(null, "address") ;
serialiaer.startTag(null, "body") ;
serialiaer.text(list.get(i).getBody()) ;
serialiaer.endTag(null, "body") ;
serialiaer.startTag(null, "date") ;
serialiaer.text(list.get(i).getDate()) ;
serialiaer.endTag(null, "date") ;
serialiaer.endTag(null, "sms") ;
}
serialiaer.endTag(null, "Smses") ;
serialiaer.endDocument() ;
Toast.makeText(this,"生成xml文件成功",0).show() ;
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this,"生成xml文件失败",0).show() ;
}
}else{
Toast.makeText(this,"sd卡不可用,请检查sd的状态",0).show() ;
}
二丶XML的解析
// 加载文件
try {
// FileInputStream fis = new FileInputStream(
// "/data/data/com.xunfang.pull/weather.xml");
InputStream fis = MainActivity.class.getClassLoader().getResourceAsStream("weather.xml") ;
// 生成xml的pull解析器
XmlPullParser pull = Xml.newPullParser();
pull.setInput(fis, "UTF-8");
int type = pull.getEventType();
Weather weather = null;
while (type != XmlPullParser.END_DOCUMENT) {
// 判断
switch (type) {
case XmlPullParser.START_TAG:
// 拿到标签的名字
String tagName = pull.getName();
if ("channel".equals(tagName)) {
weather = new Weather();
weather.setId(pull.getAttributeValue(0));
}
if ("city".equals(tagName)) {
weather.setName(pull.nextText());
}
if ("temp".equals(tagName)) {
weather.setTemp(pull.nextText());
}
if ("wind".equals(tagName)) {
weather.setWind(pull.nextText());
}
break;
case XmlPullParser.END_TAG:
String name = pull.getName();
if ("channel".equals(name)) {
// 将对象放到集合中
list.add(weather);
}
break;
}
// 获取下一个事件对象
type = pull.next();
}
} catch (Exception e) {
e.printStackTrace();
}
//打印出来
for (int i = 0; i < list.size(); i++) {
Weather w = list.get(i);
System.out.println("城市名称:" + w.getName());
System.out.println("城市id:" + w.getId());
System.out.println("城市风力:" + w.getWind());
System.out.println("城市气温:" + w.getTemp());
System.out.println("---------------------------------");
}
标签:xml xmlpullparser
原文地址:http://2585211.blog.51cto.com/10044233/1664251