<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="2011-12-06" />
<current_date_time data="2011-12-07 01:30:00 +0000" />
<unit_system data="SI" />
</forecast_information>
<current_conditions>
<condition data="雾" />
<temp_f data="28" />
<temp_c data="-2" />
<humidity data="湿度: 100%" />
<icon data="/ig/images/weather/cn_fog.gif" />
<wind_condition data="风向: 北、风速:1 米/秒" />
</current_conditions>
<forecast_conditions>
<day_of_week data="周二" />
<low data="-2" />
<high data="5" />
<icon data="/ig/images/weather/mostly_sunny.gif" />
<condition data="以晴为主" />
</forecast_conditions>
<forecast_conditions>
<day_of_week data="周三" />
<low data="-11" />
<high data="4" />
<icon data="/ig/images/weather/sunny.gif" />
<condition data="晴" />
</forecast_conditions>
<forecast_conditions>
<day_of_week data="周四" />
<low data="-9" />
<high data="0" />
<icon data="/ig/images/weather/sunny.gif" />
<condition data="晴" />
</forecast_conditions>
<forecast_conditions>
<day_of_week data="周五" />
<low data="-10" />
<high data="0" />
<icon data="/ig/images/weather/mostly_sunny.gif" />
<condition data="以晴为主" />
</forecast_conditions>
</weather>
</xml_api_reply>我用pull进行读取,第一个读取到startdocument,第二个就enddocument气死我了,不知道怎么搞的 XmlPullParser parser = XmlPullParserFactory.newInstance()
.newPullParser();
parser.setInput(is, "utf-8"); int eventType = parser.getEventType();
List<Condition> weathers = null;
while (eventType != XmlPullParser.END_DOCUMENT) {
Condition condition = null;
switch (eventType) {
case XmlPullParser.START_TAG:
weathers = new ArrayList<Condition>();
if ("current_conditions".equals(parser.getName())) {
condition = new Condition();
condition.setToday(true);
} else if ("forecast_conditions".equals(parser.getName())) {
condition = new Condition();
condition.setToday(false);
} else if ("condition".equals(parser.getName())) {
condition.setCondition(parser.getAttributeValue(null,"data"));
} else if ("day_of_week".equals(parser.getName())) {
condition.setDay_of_week(parser.getAttributeValue(null,"data"));
} else if ("low".equals(parser.getName())) {
condition.setLow(parser.getAttributeValue(null, "data"));
} else if ("high".equals(parser.getName())) {
condition.setHigh(parser.getAttributeValue(null, "data"));
} else if ("temp_c".equals(parser.getName())) {
condition.setCurrent(parser.getAttributeValue(null, "data"));
} else if ("humidity".equals(parser.getName())) {
condition.setHumidity(parser.getAttributeValue(null, "data"));
} else if ("icon".equals(parser.getName())) {
condition.setHumidity(parser.getAttributeValue(null, "data"));
} else if ("wind_condition".equals(parser.getName())) {
condition.setDay_of_week(parser.getAttributeValue(null,"data"));
condition.setWind_condition(parser.getAttributeValue(null, "data"));
} break; case XmlPullParser.END_TAG:
if ("current_conditions".equals(parser.getName())
|| "forecast_conditions".equals(parser.getName())) {
weathers.add(condition);
condition = null;
break;
}
}
/*
 * if(eventType==XmlPullParser.END_TAG){
 * if(name.equals("current_conditions"
 * )||name.equals("forecast_conditions")){ weathers.add(condition);
 * condition=null; } }
 */ eventType = parser.next();
} return weathers;

解决方案 »

  1.   

    <xxxx/>这样的标签算是开始还是结束标签啊= =真郁闷啊
      

  2.   

    public boolean isEmptyElementTag() throws XmlPullParserException    Returns true if the current event is START_TAG and the tag is degenerated (e.g. <foobar/>).    NOTE: if parser is not on START_TAG then the exception will be thrown.为什么要用 xmlpull,这种非常适合用xpath提取。
      

  3.   

        public static List<Map<String,String>> getWeatherInfo(){
            final HashSet<String> TAGS1 = new HashSet<>(Arrays.<String>asList("forecast_information","current_conditions","forecast_conditions"));
            final HashSet<String> TAGS2 = new HashSet<>(Arrays.<String>asList("city", "postal_code", "forecast_date", "current_date_time", "unit_system", "condition", "temp_c", "temp_f", "humidity", "icon", "wind_condition", "day_of_week", "low", "high"));
            try (BufferedReader reader = Files.newBufferedReader(Paths.get("/tmp","weather.xml"),Charset.forName("UTF-8"))) {
                    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
                    XmlPullParser parser = factory.newPullParser();
                    parser.setInput(reader);                List<Map<String,String>> conditions = new ArrayList<>();
                    Map<String, String> condition = null;                for (int eventType = parser.getEventType(); ;eventType = parser.next()) {
                        switch (eventType) {
                        case XmlPullParser.START_DOCUMENT:
                            break;
                        case XmlPullParser.END_DOCUMENT:
                            return conditions;
                        case XmlPullParser.START_TAG:
                            {
                                String tag = parser.getName();
                                if (TAGS1.contains(tag)) {
                                    condition = new LinkedHashMap<String,String>();
                                    condition.put("TAG",tag);
                                    break;
                                }
                                if (TAGS2.contains(tag)) {
                                    condition.put(parser.getName(),parser.getAttributeValue(0));
                                    break;
                                }
                            }
                            break;
                        case XmlPullParser.END_TAG:
                            {
                                if (TAGS1.contains(parser.getName())) {
                                    conditions.add(condition);
                                }
                            }
                            break;
                        case XmlPullParser.TEXT:
                            break;
                        default:
                            break;
                        }
                    }
                } catch (final IOException|XmlPullParserException e) {
            }
            return null;
        }
        public static void main(final java.lang.String[] args) {
            for (Map<String,String> condition: getWeatherInfo()) {
                System.out.println(condition);
            }
        }
    jdk7u1 输出:
    {TAG=forecast_information, city=Beijing, Beijing, postal_code=Beijing, forecast_date=2011-12-06, current_date_time=2011-12-07 01:30:00 +0000, unit_system=SI}
    {TAG=current_conditions, condition=雾, temp_f=28, temp_c=-2, humidity=湿度: 100%, icon=/ig/images/weather/cn_fog.gif, wind_condition=风向: 北、风速:1 米/秒}
    {TAG=forecast_conditions, day_of_week=周二, low=-2, high=5, icon=/ig/images/weather/mostly_sunny.gif, condition=以晴为主}
    {TAG=forecast_conditions, day_of_week=周三, low=-11, high=4, icon=/ig/images/weather/sunny.gif, condition=晴}
    {TAG=forecast_conditions, day_of_week=周四, low=-9, high=0, icon=/ig/images/weather/sunny.gif, condition=晴}
    {TAG=forecast_conditions, day_of_week=周五, low=-10, high=0, icon=/ig/images/weather/mostly_sunny.gif, condition=以晴为主}
      

  4.   

    我大概知道哪里错了你那种做法很好,我一直以为对待xml就要用对象封装里面的数据,没想到你这种用在这里更加合适!