Search code examples
androidxml-parsingcdatadomparser

Android: How to display named character entities from CDATA elements while parsing with XML DOM parser?


While using the android DOM parser, I'm picking up the following element:

<![CDATA[
Widget1&#8230; Widget2&#8230; Widget3&#8230;
]]>

I've tried populating a textview using Html.fromHtml as well as trying to shove the contents of that element into a Webview.

both methods display the content, but seems to strip out the named character entities

Anything I can do to retain the formatting/markup?

I am reading the data from an RSS feed if that helps.

this is what my webview instantiation looks like

webview.loadData(((Node) nodeList.item(0)).getNodeValue(),  "text/html", "utf-8");

Solution

  • I had a similar issue recently and here's what worked for me. I used the jsoup.jar from http://jsoup.org/. Once I added the jar to my project, I just needed to use the following line:

    String htmlFreeString = Jsoup.parse( stringWithHtml ). text();
    

    Then, I just set the string as the text to my TextView without issue.

    Hope that helps.