Android解码xml文件中的html

标签 android html xml decoding

在我的软件中,我收到一个 xml 文件,其中包含一些 HTML 实体,例如 &管他呢。 我成功解码了 xml 但不是 HTML 实体。当遇到 html 实体时,字符串会被切断......有人可以帮忙吗?我实际上有这样的代码来解码 xml...

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
   DocumentBuilder builder = factory.newDocumentBuilder();
InputStream inputStream = entity.getContent();
Document dom = builder.parse(inputStream);
   inputStream.close();


   Element racine = dom.getDocumentElement();
   NodeList nodeLst=racine.getElementsByTagName("product");

有谁知道我如何做同样的工作,将 xml 解码为 dom 对象并解码 HTML 实体?

实际上我的 dom 对象不正确,因为它包含一些因 HTML 实体而被剪切的字符串...我该怎么办?

最佳答案

我有两种建议:

  1. 停用验证:factory.setValidating(false);

  2. 将 XHTML DTD 标记添加到您的 XML 流中,紧跟在 <?xml ...> 之后标签。

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

关于Android解码xml文件中的html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4131906/

相关文章:

jquery - 基于设备分辨率的 Bootstrap 3 嵌入图像大小

android - 此处不允许使用 JetBrain Idea android xml 元素

android - 在操作栏中填充水平自定义 View

android - 来自一个 xml 文件的多个可绘制对象

android - 如何正确修复位于 CoordinatorLayout 中的页脚

javascript - 在 Canvas 上旋转 2 个图像

android - 带有 Thumb Android 的自定义 ProgressBar

php - header css/bootstrap 问题,奇怪

java - 解析 XML,跳过某些标签

python xml.etree.ElementTree 附加到子元素