Why do I get this ClassCastException error?

3,187 views
Skip to first unread message

Ine

unread,
Jan 6, 2012, 12:47:34 PM1/6/12
to vogella
I get a runtime error using:

RSS feeds with Java - Tutorial
(from: http://www.vogella.de/articles/RSSFeed/article.html)

Exception I get is:

Exception in thread "main" java.lang.ClassCastException:
com.sun.xml.internal.stream.events.EndElementEvent cannot be cast to
javax.xml.stream.events.Characters
at com.sun.xml.internal.stream.events.DummyEvent.asCharacters(Unknown
Source)

The feed being read is:
Feed: http://feeds.feedburner.com/zeefeed?fmt=xml

Additional info: I use Eclipse Indigo
Version: Indigo Release
Build id: 20110615-0604
Java 6 build 1.6.0_29-b11

What might be the problem?

Thank you.

Lars Vogel

unread,
Jan 7, 2012, 6:26:23 AM1/7/12
to vog...@googlegroups.com
I assume you imported the incorrect class.

2012/1/6 Ine <ine...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "vogella" group.
To post to this group, send email to vog...@googlegroups.com.
To unsubscribe from this group, send email to vogella+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/vogella?hl=en.




--
Lars
http://www.vogella.de - Eclipse, Android and Java Tutorials
http://www.twitter.com/vogella - Lars on Twitter

Ine

unread,
Jan 9, 2012, 8:21:07 AM1/9/12
to vogella
The imports were copied directly from the source in the tutorial
( http://www.vogella.de/articles/RSSFeed/article.html ), them being:

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.XMLEvent;

I never changed them.

Lars Vogel

unread,
Jan 9, 2012, 11:26:27 AM1/9/12
to vog...@googlegroups.com

can you please post the whole class?

Ine

unread,
Jan 17, 2012, 8:05:17 AM1/17/12
to vogella
Of course:

package nuevoModelo;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.XMLEvent;


public class RSSFeedParser {
static final String TITLE = "title";
static final String DESCRIPTION = "description";
static final String CHANNEL = "channel";
static final String LANGUAGE = "language";
static final String COPYRIGHT = "copyright";
static final String LINK = "link";
static final String AUTHOR = "author";
static final String ITEM = "item";
static final String PUB_DATE = "pubDate";
static final String GUID = "guid";

final URL url;
final URL iconURL;

public RSSFeedParser(String feedUrl, String feedIconUrl) {
try {
this.url = new URL(feedUrl);
this.iconURL = new URL(feedIconUrl);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}

public Feed readFeed() {
Feed feed = null;
try {

boolean isFeedHeader = true;
// Set header values intial to the empty string
String description = "";
String title = "";
String link = "";
String language = "";
String copyright = "";
String author = "";
String pubdate = "";
String guid = "";

// First create a new XMLInputFactory
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
// Setup a new eventReader
InputStream in = read();
XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
// Read the XML document
while (eventReader.hasNext()) {

XMLEvent event = eventReader.nextEvent();

if (event.isStartElement()) {
if (event.asStartElement().getName().getLocalPart() == (ITEM)) {
if (isFeedHeader) {
isFeedHeader = false;
feed = new Feed(title, link, description, language,
copyright, pubdate, iconURL);
}
event = eventReader.nextEvent();
continue;
}

if (event.asStartElement().getName().getLocalPart() == (TITLE)) {
event = eventReader.nextEvent();
title = event.asCharacters().getData();
continue;
}
if (event.asStartElement().getName().getLocalPart() ==
(DESCRIPTION)) {
event = eventReader.nextEvent();
description = event.asCharacters().getData();
continue;
}

if (event.asStartElement().getName().getLocalPart() == (LINK)) {
event = eventReader.nextEvent();
//FIXME ACA ESTA EL PROBLEMA:
link = event.asCharacters().getData();
continue;
}

if (event.asStartElement().getName().getLocalPart() == (GUID)) {
event = eventReader.nextEvent();
guid = event.asCharacters().getData();
continue;
}
if (event.asStartElement().getName().getLocalPart() ==
(LANGUAGE)) {
event = eventReader.nextEvent();
language = event.asCharacters().getData();
continue;
}
if (event.asStartElement().getName().getLocalPart() == (AUTHOR))
{
event = eventReader.nextEvent();
author = event.asCharacters().getData();
continue;
}
if (event.asStartElement().getName().getLocalPart() ==
(PUB_DATE)) {
event = eventReader.nextEvent();
pubdate = event.asCharacters().getData();
continue;
}
if (event.asStartElement().getName().getLocalPart() ==
(COPYRIGHT)) {
event = eventReader.nextEvent();
copyright = event.asCharacters().getData();
continue;
}
} else if (event.isEndElement()) {
if (event.asEndElement().getName().getLocalPart() == (ITEM)) {
FeedItem message = new FeedItem();
message.setAuthor(author);
message.setDescription(description);
message.setGuid(guid);
message.setLink(link);
message.setTitle(title);
feed.getMessages().add(message);
event = eventReader.nextEvent();
continue;
}
}
}
} catch (XMLStreamException e) {
throw new RuntimeException(e);
}
return feed;

}

private InputStream read() {
try {
return url.openStream();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

Lars Vogel

unread,
Jan 20, 2012, 10:28:02 AM1/20/12
to vog...@googlegroups.com
Hi,

I check my example and it still works. Please try to run the test from: http://www.vogella.de/articles/RSSFeed/article.html#read_test

I assume the RSS stream you are reading is different from the one I created this example for.
Best regards, Las 

2012/1/9 Ine <ine...@gmail.com>



--

Lars Vogel

unread,
Mar 6, 2013, 2:34:54 PM3/6/13
to vog...@googlegroups.com
Thanks, looks also much cleaner. I think I adjust the tutorial accordingly. 

Cheers, Lars

2013/3/1 Christian.Meister <christia...@gmx.eu>
I had the same problem and modified the class.
It now works for me.


public class RSSFeedParser {
    static final String TITLE = "title";
    static final String DESCRIPTION = "description";
    static final String CHANNEL = "channel";
    static final String LANGUAGE = "language";
    static final String COPYRIGHT = "copyright";
    static final String LINK = "link";
    static final String AUTHOR = "author";
    static final String ITEM = "item";
    static final String PUB_DATE = "pubDate";
    static final String GUID = "guid";

    final URL url;

    public RSSFeedParser(String feedUrl) {

        try {
            this.url = new URL(feedUrl);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
    }

    public Feed readFeed() {
        Feed feed = null;
        try {
            boolean isFeedHeader = true;
            // Set header values intial to the empty string
            String description = "";
            String title = "";
            String link = "";
            String language = "";
            String copyright = "";
            String author = "";
            String pubdate = "";
            String guid = "";

            // First create a new XMLInputFactory
            XMLInputFactory inputFactory = XMLInputFactory.newInstance();
            // Setup a new eventReader
            InputStream in = read();
            XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
            // Read the XML document
            while (eventReader.hasNext()) {
                XMLEvent event = eventReader.nextEvent();
                if (event.isStartElement()) {
                    String localPart = event.asStartElement().getName().getLocalPart();
                    switch (localPart) {
                        case ITEM :

                            if (isFeedHeader) {
                                isFeedHeader = false;
                                feed = new Feed(title, link, description, language, copyright, pubdate);
                            }
                            event = eventReader.nextEvent();
                            break;
                        case TITLE :
                            title = getCharacterData(event, eventReader);
                            break;
                        case DESCRIPTION :
                            description = getCharacterData(event, eventReader);
                            break;
                        case LINK :
                            link = getCharacterData(event, eventReader);
                            break;
                        case GUID :
                            guid = getCharacterData(event, eventReader);
                            break;
                        case LANGUAGE :
                            language = getCharacterData(event, eventReader);
                            break;
                        case AUTHOR :
                            author = getCharacterData(event, eventReader);
                            break;
                        case PUB_DATE :
                            pubdate = getCharacterData(event, eventReader);
                            break;
                        case COPYRIGHT :
                            copyright = getCharacterData(event, eventReader);
                            break;

                    }
                } else if (event.isEndElement()) {
                    if (event.asEndElement().getName().getLocalPart() == (ITEM)) {
                        FeedMessage message = new FeedMessage();

                        message.setAuthor(author);
                        message.setDescription(description);
                        message.setGuid(guid);
                        message.setLink(link);
                        message.setTitle(title);
                        feed.getMessages().add(message);
                        event = eventReader.nextEvent();
                        continue;
                    }
                }
            }
        } catch (XMLStreamException e) {
            throw new RuntimeException(e);
        }
        return feed;
    }

    private String getCharacterData(XMLEvent event, XMLEventReader eventReader) throws XMLStreamException {
        String result = "";
        event = eventReader.nextEvent();
        if (event instanceof CharacterEvent) {
            result = event.asCharacters().getData();
        }
        return result;

    }

    private InputStream read() {
        try {
            return url.openStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
--
You received this message because you are subscribed to the Google Groups "vogella" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vogella+u...@googlegroups.com.

To post to this group, send email to vog...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages