当“SAXParseException”发生时用于 XML 中

发布于 2024-12-09 11:40:19 字数 543 浏览 0 评论 0原文

我收到“org.xml.sax.SAXParseException;lineNumber:4;columnNumber:26;实体“ldquo”被引用,但未声明。”读取 XML 文档时出现异常。我的阅读方式如下:

                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                DocumentBuilder builder = factory.newDocumentBuilder();
                InputSource is = new InputSource(new StringReader(xmlBody));
                Document document = builder.parse(is);

然后 builder.parse(is); 上有一个例外 通过搜索,我认为有必要在外部声明其中一些新实体,不幸的是,我无法修改原始 XML 文档。

我该如何解决这个问题?

谢谢

I'm getting a "org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 26; The entity "ldquo" was referenced, but not declared." exception when reading an XML document. I'm reading it as follows:

                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                DocumentBuilder builder = factory.newDocumentBuilder();
                InputSource is = new InputSource(new StringReader(xmlBody));
                Document document = builder.parse(is);

And then there's an exception on builder.parse(is);
From searching I figured that it is necessary to declare some of those new entities externally, unfortunately, I cannot modify the original XML document.

How do I fix this problem?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

携余温的黄昏 2024-12-16 11:40:19

通过搜索,我发现有必要在外部声明其中一些新实体,不幸的是,我无法修改原始 XML 文档。

好吧,除非您声明该实体,否则该文档就不是 XML,并且您将无法使用 XML 解析器对其进行处理。

当您被要求处理不是格式良好的 XML 的输入时,最好的方法是修复创建文档的过程(使用 XML 进行交换的整个想法依赖于它是格式良好的 XML)。另一种选择是“修复”文档,将其转换为格式良好的 XML(您说您做不到),或者忘记它本来就是 ​​XML 的事实,并像处理任何专有的非 XML 文档一样对待它。 -XML 格式。

这不是一组令人愉快的选择 - 但这就是当人们口头上支持 XML 但不符合标准的字面意思时所陷入的混乱。

From searching I figured that it is necessary to declare some of those new entities externally, unfortunately, I cannot modify the original XML document.

Well, unless you declare the entity then the document isn't XML and you won't be able to process it using an XML parser.

When you are asked to process input that isn't well-formed XML, the best approach is to fix the process that created the document (the whole idea of using XML for interchange relies on it being well-formed XML). The alternatives are to "repair" the document to turn it into well-formed XML (which you say you can't do), or to forget the fact that it was intended to be XML, and treat it as you would any proprietary non-XML format.

Not a pleasant set of choices - but that's the mess you get into when people pay lip-service to XML but fail to conform to the letter of the standard.

乄_柒ぐ汐 2024-12-16 11:40:19

尝试

factory.setExpandEntityReferences(false);

这将阻止解析器尝试扩展实体。

编辑:这个怎么样 http://xerces。 apache.org/xerces2-j/features.html#dom.create-entity-ref-nodes——该页面的顶部有一个如何在底层解析器上设置功能的示例。这应该会导致解析器创建实体引用 DOM 节点,而不是尝试扩展实体。

Try

factory.setExpandEntityReferences(false);

This will prevent the parser from trying to expand entities.

EDIT: How about this http://xerces.apache.org/xerces2-j/features.html#dom.create-entity-ref-nodes -- The top of that page has an example of how to set features on the underlying parser. This should cause the parser to create entity-reference DOM nodes instead of trying to expand the entities.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文