android pull解析子节点问题

Qiqihar 2012-03-30 07:51:22
问题如下:
* weather.xml中的current_conditions,forecast_conditions这两个子节点内有相同的子节点 icon、condition,这种情况怎么解析???
这里只对子节点<forecast_conditions>下的节点内容进行了解析,注意代码的红色部分。

下面是xml文件和pull解析代码的实现,请大侠们帮忙。

xml文件:
<?xml version="1.0" encoding="utf-8"?>
<xml_api_reply version="1">
<weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1"
row="0" section="0">
<forecast_information>
<city data="Beijing, Beijing" />
<postal_code data="beijing" />
<latitude_e6 data="" />
<longitude_e6 data="" />
<forecast_date data="2012-03-30" />
<current_date_time data="2012-03-30 20:30:00 +0000" />
<unit_system data="SI" />
</forecast_information>
<current_conditions>
<condition data="晴" />
<temp_f data="55" />
<temp_c data="13" />
<humidity data="湿度: 12%" />
<icon data="/ig/images/weather/sunny.gif" />
<wind_condition data="风向: 北、风速:9 米/秒" />
</current_conditions>
<forecast_conditions>
<day_of_week data="周五" />
<low data="2" />
<high data="13" />
<icon data="/ig/images/weather/mostly_sunny.gif" />
<condition data="以晴为主" />
</forecast_conditions>
<forecast_conditions>
<day_of_week data="周六" />
<low data="1" />
<high data="14" />
<icon data="/ig/images/weather/sunny.gif" />
<condition data="晴" />
</forecast_conditions>
<forecast_conditions>
<day_of_week data="周日" />
<low data="1" />
<high data="8" />
<icon data="/ig/images/weather/mostly_sunny.gif" />
<condition data="以晴为主" />
</forecast_conditions>
<forecast_conditions>
<day_of_week data="周一" />
<low data="2" />
<high data="15" />
<icon data="/ig/images/weather/mostly_sunny.gif" />
<condition data="以晴为主" />
</forecast_conditions>
</weather>
</xml_api_reply>


pull解析方法:

public List<Other> getXml(InputStream in) throws IOException {

try {
// 声明XmlPullParserFactory
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
// 通过XmlPullParserFactory获取XmlPullParser
XmlPullParser parser = factory.newPullParser();

List<Other> others = null;
Other other = null;
// 设置InputStream用于解析
parser.setInput(in, "utf-8");
// 标签状态
int event = parser.getEventType();

while (event != XmlPullParser.END_DOCUMENT) {
String nodeName = parser.getName();
switch (event) {
// xml开始
case XmlPullParser.START_DOCUMENT:
others = new ArrayList<Other>();
break;

case XmlPullParser.START_TAG:
if (nodeName.equals("forecast_conditions")) {
other = new Other();
}
if (nodeName.equals("day_of_week")) {
other.setDay(parser.getAttributeValue(0));
}
// ===============取消注释后报NullpointerException=============
//if (nodeName.equals("icon")) {
// other.setOurl(parser.getAttributeValue(0));
//}
if (nodeName.equals("low")) {
other.setLow(parser.getAttributeValue(0));
}
if (nodeName.equals("high")) {
other.setHigh(parser.getAttributeValue(0));
}
// ===============取消注释后报NullpointerException===============
//if (nodeName.equals("condition")) {
// other.setOcondition(parser.getAttributeValue(0));
//}
break;

// 结束
case XmlPullParser.END_TAG:
if (nodeName.equals("forecast_conditions") && other != null) {
others.add(other);
other = null;
} else {
System.out.println("other = null");
}
break;
default:
break;
}
// event下移继续循环
event = parser.next();
}
return others;

} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
}




...全文
408 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
danielinbiti 2013-08-29
  • 打赏
  • 举报
回复
定义标记位,在start_tag时判断是哪个开始,在end_tag时清除标记,在解析子节点时都增加标记位的判断
无网_12 2013-08-29
  • 打赏
  • 举报
回复
把它们看做2个实体类,里面的字节当做属性,然后用SAX解析。
aldifoadfnakld 2013-08-27
  • 打赏
  • 举报
回复
我也想知道,坐等

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧