您好,登錄后才能下訂單哦!
xml的pull解析:
//類加載器加載xml文件
InputStream is = MainActivity.class.getClassLoader().getResourceAsStream("weather.xml");
//生成xml的pull解析器
XmlPullParser pull = Xml.newPullParser();
try {
//設置輸入流
pull.setInput(is, "utf-8");
//解析器當前處于的狀態
int type = pull.getEventType();
Weather weather = null ;
while (type != XmlPullParser.END_DOCUMENT) {
switch (type) {
//當前是開始標簽的節點
case XmlPullParser.START_TAG:
//拿到節點標簽的名字
String name = pull.getName();
//判斷節點標簽的名字 (下面同這兒),然后放到weather對象中
if("channel".equals(name)){
weather = new Weather();
weather.setId(pull.getAttributeValue(0));
}
if("city".equals(name)){
weather.setName(pull.nextText());
}
if("temp".equals(name)){
weather.setTemp(pull.nextText());
}
if("wind".equals(name)){
weather.setWind(pull.nextText());
}
break;
//如果是結束標簽的節點,此處將weather對象放入list集合保存
case XmlPullParser.END_TAG:
String tagname = pull.getName();
if("channel".equals(tagname)){
list.add(weather);
}
default:
break;
}
//此處必須寫,否則不會向下執行
type = pull.next();
}
} catch (Exception e) {
e.printStackTrace();
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。