在我的软件中,我收到一个 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 实体而被剪切的字符串...我该怎么办?
最佳答案
我有两种建议:
停用验证:
factory.setValidating(false);
将 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/