【问题标题】:Android parsing HTML entities using DOM parser for RSS feedAndroid 使用 DOM 解析器为 RSS 提要解析 HTML 实体
【发布时间】:2025-12-18 07:15:02
【问题描述】:

我正在为我正在构建的 Android 应用程序使用 google books api。这是 XML 文件的示例

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

这是我用来提取描述的一小部分代码

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

}

问题是当使用 HTML.fromHtml(Str) 函数时,它会在第一个 HTML 实体处截断文本(所以在这个例子中它只是简单地说

这三部曲包括

当我在 Android 之外运行相同的代码时,它可以正常工作,并且至少显示带有转义字符的字符串,即

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

如果我随后手动将其添加到应用程序中,HTML.fromHtml() 工作正常,所以我猜问题是 Android 的解析器实现。

类似的问题是Android decoding html in xml file。我尝试将工厂的验证设置为 false,因为它是一个 RSS 提要,所以我无法声明 HTML 根元素(如本文所建议的那样)。

【问题讨论】:

    标签: java android html-parsing rss domparser


    【解决方案1】:

    我最终没有从 Google 获得描述数据,但我认为可以通过在文档元素上运行 normalise() 来解决这个问题 - 我在另一个 API 上遇到了类似的问题,并且修复了它。

    【讨论】:

    • 我有同样的问题,你能解释一下“normalise()”是什么意思吗?