xml的pull解析:
10年積累的成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有濱州免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
//類加載器加載xml文件
InputStream is = MainActivity.class.getClassLoader().getResourceAsStream("weather.xml");
//生成xml的pull解析器
XmlPullParser pull = Xml.newPullParser();
try {
//設(shè)置輸入流
pull.setInput(is, "utf-8");
//解析器當(dāng)前處于的狀態(tài)
int type = pull.getEventType();
Weather weather = null ;
while (type != XmlPullParser.END_DOCUMENT) {
switch (type) {
//當(dāng)前是開始標(biāo)簽的節(jié)點(diǎn)
case XmlPullParser.START_TAG:
//拿到節(jié)點(diǎn)標(biāo)簽的名字
String name = pull.getName();
//判斷節(jié)點(diǎn)標(biāo)簽的名字 (下面同這兒),然后放到weather對(duì)象中
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;
//如果是結(jié)束標(biāo)簽的節(jié)點(diǎn),此處將weather對(duì)象放入list集合保存
case XmlPullParser.END_TAG:
String tagname = pull.getName();
if("channel".equals(tagname)){
list.add(weather);
}
default:
break;
}
//此處必須寫,否則不會(huì)向下執(zhí)行
type = pull.next();
}
} catch (Exception e) {
e.printStackTrace();
}
}