问题如下:
 * 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;
}