2

RSS フィードからノードを解析しようとしていchannelますが、次のエラーが発生し続けますparser.nextText():

org.xmlpull.v1.XmlPullParserException: precondition: START_TAG (position:END_TAG </link>@3:449 in java.io.InputStreamReader@7988a7d) 

問題はparser.getEventType()、2 (START_TAG) であるべきところが 3 (END_TAG) であるようです。

餌:

<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
    <channel>
        <title>Podcast Title</title>
        <link>http://www.link.com</link>
        <description>A description</description>

          <item>
          </item>

          <item>
          </item>

          <item>
          </item>

    </channel>
</rss>

コード:

    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(false);
    XmlPullParser parser = factory.newPullParser();
    InputStream stream = new URL(url).openConnection().getInputStream();
    parser.setInput(stream, "UTF-8");
    Boolean inChannel = false;

    int eventType = parser.getEventType();
    while (eventType != XmlPullParser.END_DOCUMENT) {
        String name;
        switch (eventType) {
            case XmlPullParser.START_DOCUMENT:
                break;
            case XmlPullParser.START_TAG:
                name = parser.getName();
                if (name.equalsIgnoreCase("channel"))
                    inChannel = true;
                else if (inChannel)
                {
                    channel = new ChannelItem();
                    if (name.equalsIgnoreCase("description"))
                        channel.setDescription(parser.nextText().trim());
                    else if (name.equalsIgnoreCase("media:thumbnail"))
                        channel.setThumnailUrl(parser.getAttributeValue(null, "url"));
                }
                break;
            case XmlPullParser.END_TAG:
                name = parser.getName();
                if (name.equalsIgnoreCase("channel"))
                    inChannel = false;
                break;
        }
        eventType = parser.next();
    }
4

2 に答える 2