开发者

Android parsing HTML entities using DOM parser for RSS feed

开发者 https://www.devze.com 2023-02-03 05:27 出处:网络
I am using the google books api for an Android app that I am building. This is a sample of the XML file

I am using the google books api for an Android app that I am building. This is a sample of the XML file

<dc:description>This trilogy includes &amp;quot; The Hitchhiker&amp;#39;s Guide to the Galaxy&amp;quot; , &amp;quot; TheRestaurant at the End of the Universe&amp;quot; , &amp;quot; Life, Universe and Everything&amp;quot; and &amp;quot; So Long ...</dc:description>
<dc:format>590 pages</dc:format>
<dc:format>book</dc:format>

And this is a fraction of the code I'm using to extract the description

if ( entry.getElementsByTagName( "dc:description" ).item( 0 ) != null ) {
  Element d = ( Element ) entry.getElementsByTagName( "dc:description" )
      .item( 0 );
  b.setDescription( d.getFirstChild( ).getNodeValue( ) );

}

The problem is when using the HTML.fromHtml(Str) function it cuts off the text at the first HTML entity (so in this example it says simply

This trilogy includes

When I run the same code outside of Android it works ok and at least shows the string with the escape characters, i.e.

This trilogy includes &quot; The Hitchhiker&#39;s Guide to the Galaxy&quot; , &quot; TheRestaurant at the End of the Universe&quot; , &quot; Life, Universe and Everything&quot; and &quot; So Long ...

If I then manually add this to the app the HTML.fromHtml() works fine so I guess the problem is Android's implementation of the parser.

A similar pr开发者_运维百科oblem is Android decoding html in xml file. I have tried setting the validation of the factory to false, and as it is an RSS feed I cannot declare an HTML root element (as suggested in this post).


I ended up not getting the description data from Google but I think the problem might be solved by running normalise() on the document element - I had a similar problem with another API and that fixed it.

0

精彩评论

暂无评论...
验证码 换一张
取 消