user1154644
user1154644

Reputation: 4609

Handling CDATA when parsing RSS Feed with JAVA

I followed Vogella's tutorial for parsing an RSS Feed using JAVA. The code is straightforward and I was able to get it to work. The problem is some of the nodes im parsing have CDATA, and I'm getting empty strings (based on the way the parser is implemented).

In short, my question is, is there an easy way to modify this implementation to handle CDATA?

Vogella RSS Parser

Upvotes: 0

Views: 598

Answers (1)

janih
janih

Reputation: 2234

It handles CDATA, the parser unfortunately just returns the value after reading the first line, so in cases like this

<description>
  <![CDATA[
  Lorem ipsum..
  ]]>
</description>

It will not read until the end of the element. You should change the RSSFeedParser.getCharacterData method to something like this:

private String getCharacterData(XMLEvent event, XMLEventReader eventReader)
        throws XMLStreamException {
    StringBuilder result = new StringBuilder();
    while (!(event = eventReader.nextEvent()).isEndElement()) {
        if (event instanceof Characters) {
            result.append(event.asCharacters().getData());
        }
    }
    return result.toString();
}

Now the content of description tag will be "\nLorem ipsum..\n"

Upvotes: 1

Related Questions