2

I'm trying to parse the channel node from an RSS feed but I keep getting this error thrown at parser.nextText():

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

The problem seems to be that parser.getEventType() is 3 (END_TAG), when it should be 2 (START_TAG)

Feed:

<?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>

Code:

    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();
    }
2
  • The code works fine in my IDE with parser.nextText() although the call to getAttributeValue looks wrong Commented Sep 25, 2017 at 6:21
  • Interesting. I tried everything and couldn't get it to work. The code I posted below works for what I need. Commented Sep 29, 2017 at 18:57

2 Answers 2

0

I gave up and ended up using a this:

  final ChannelItem channel = new ChannelItem();

    final RootElement root = new RootElement("rss");
    final Element channelNode = root.getChild("channel");


    channelNode.getChild("title").setEndTextElementListener(new EndTextElementListener()
    {
        public void end(final String body) {
            channel.setTitle(body);
        }
    });

    channelNode.getChild("link").setEndTextElementListener(new EndTextElementListener()
    {
        public void end(final String body) {
            channel.setSiteUrl(body);
        }
    });


    channelNode.getChild("description").setEndTextElementListener(new EndTextElementListener()
    {
        public void end(final String body) {
            channel.setDescription(body);
        }
    });

    try
    {
        InputStream stream = new URL(url).openConnection().getInputStream();

        Xml.parse(stream, Xml.Encoding.UTF_8, root.getContentHandler());
        stream.close();
    }
    catch (Exception e)
    {
    }
    return channel;
Sign up to request clarification or add additional context in comments.

Comments

0

Parse XML with Jsoup

Xml Sample

<?xml version="1.0" encoding="UTF-8">
<tests>
    <test>
        <id>xxx</id>
        <status>xxx</status>
    </test>
    <test>
        <id>xxx</id>
        <status>xxx</status>
    </test>
    ....
</tests>

Code Sample

String html = "<?xml version=\"1.0\" encoding=\"UTF-8\"><tests><test><id>xxx</id><status>xxx</status></test><test><id>xxx</id><status>xxx</status></test></tests></xml>";
Document doc = Jsoup.parse(html, "", Parser.xmlParser())
for (Element e : doc.select("test")) {
    System.out.println(e);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.