Exception while parsing xml in android

时间秒杀一切 提交于 2019-12-24 12:03:56

问题


I try to parse xml from this source

Following is my parsing code:

while (eventType != XmlPullParser.END_DOCUMENT) {
            switch (eventType) {
                case XmlPullParser.START_TAG:
                    if (parser.getName().equals("Title")) {
                        current_tag = tag_title;
                        deal = new Deals();
                    }
                    if (parser.getName().equals("MSRP")) {
                        current_tag = tag_msrp;
                    }
                    if (parser.getName().equals("ConvertedCurrentPrice")) {
                        current_tag = tag_convertedprice;
                    }
                    if (parser.getName().equals("SavingsRate")) {
                        current_tag = tag_savings;
                    }
                    if (parser.getName().equals("SmallPictureURL")) {
                        current_tag = tag_icon;
                    }

                    break;
                case XmlPullParser.END_TAG:
                    if (parser.getName().equals("Item")) {
                        current_tag = -1;
                        deals.add(deal);
                    }
                    break;
                case XmlPullParser.TEXT:
                    String value = parser.getText();
                    switch (current_tag) {
                        case tag_title:
                            deal.setTitle(value);
                            break;
                        case tag_msrp:
                            deal.setMSRP(value);
                            break;
                        case tag_convertedprice:
                            deal.setconvertedPrice(value);
                            break;
                        case tag_savings:
                            deal.setSavings(value);
                            break;
                        case tag_icon:
                            System.out.println("Icon for the current deal is: "+value);
                            deal.setIcon(value);
                            break;
                        default:
                            break;
                    }
                    current_tag = -1;
                    break;
            }
            eventType = parser.next();
        }
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

At the line eventType = parser.next() I get the following exception:

07-19 10:43:23.262: W/System.err(922): org.xmlpull.v1.XmlPullParserException: expected: /link read: head (position:END_TAG </head>@1:427 in java.io.InputStreamReader@40dee498) 
07-19 10:43:23.272: W/Trace(922): Unexpected value from nativeGetEnabledTags: 0
07-19 10:43:23.272: W/Trace(922): Unexpected value from nativeGetEnabledTags: 0
07-19 10:43:23.282: W/System.err(922):  at org.kxml2.io.KXmlParser.readEndTag(KXmlParser.java:970)
07-19 10:43:23.282: W/System.err(922):  at org.kxml2.io.KXmlParser.next(KXmlParser.java:372)
07-19 10:43:23.282: W/System.err(922):  at org.kxml2.io.KXmlParser.next(KXmlParser.java:310)
07-19 10:43:23.282: W/System.err(922):  at com.nykkos.dailydeals.Parsers.parseDeals(Parsers.java:91)
07-19 10:43:23.282: W/System.err(922):  at com.nykkos.dailydeals.WebServices.downloadDeals(WebServices.java:37)
07-19 10:43:23.282: W/System.err(922):  at com.nykkos.dailydeals.DealsActivity.run(DealsActivity.java:59)

I do not find any obvious reasons as to why this happens.

Any help in resolving this is much appreciated.


回答1:


It is very likely that you don't try to parse the XML that you think. Instead you seem to parse a HTML document, which is not well formed. This happens because some of the HTML tags don't require closing tags and you actually run into one of them like in the following example:

<head>
     <link rel="stylesheet" type="text/css" href="theme.css">
</head>

So please make sure that you really try to parse the XML that you intend to. Either check the URL that you try to get the XML from. The link that you provided should actually work but maybe you have a typo in your code.

Additionally debug your code and check what's inside your InputStream. I guess inside there's something very different from what you expect.



来源:https://stackoverflow.com/questions/17744826/exception-while-parsing-xml-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!