简体   繁体   中英

Exception while parsing xml in android

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM