如何使用 Java 和 Xerces 解析符合 1.1 规范的 XML?

发布于 2025-01-06 09:41:18 字数 1351 浏览 1 评论 0原文

我正在尝试解析一个包含符合 XML 1.1 规范 的 XML 内容的字符串。 XML 包含 XML 1.0 规范中不允许但 XML 1.1 规范中允许的字符引用(转换为 U+0001–U+001F 范围内的 Unicode 字符的字符引用)。

根据 Xerces2 网站,Xerces2 解析器支持解析 XML 1.1 文档。但是,我不知道如何告诉它我们尝试解析的 XML 包含符合 1.1 的 XML。

我正在使用 DocumentBuilder 来解析 XML(类似这样):

public Element parseString(String xmlString) {
    try {
          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
          DocumentBuilder documentBuilder = dbf.newDocumentBuilder();

          InputSource source = new InputSource(new StringReader(xmlString));

      // Throws org.xml.sax.SAXParseException becuase of the invalid character refs
          Document doc = documentBuilder.parse(source);

          return doc.getDocumentElement();

    } catch (ParserConfigurationException pce) {
          // Handle the error
    } catch (SAXException se) {
          // Handle the error
    } catch (IOException ioe) {
          // Handle the error
    }
}

我尝试设置 XML 标头以指示 XML 符合 1.1 规范...

xmlString = "<?xml version=\"1.1\" encoding=\"UTF-8\" ?>" + xmlString;

...但它仍然被解析为 1.0 XML(仍然生成无效字符引用异常)。

如何配置 Xerces 解析器将 XML 解析为 XML 1.1?是否有替代解析器可以为 XML 1.1 提供更好的支持?

I'm trying to parse a String which contains XML content which conforms to the XML 1.1 spec. The XML contains character references which are not allowed in the XML 1.0 spec but which are allowed in the XML 1.1 spec (character references which translate to Unicode characters in the range U+0001–U+001F).

According the Xerces2 website, the Xerces2 parser supports parsing XML 1.1 documents. However, I cannot figure out how to tell it the XML we are trying to parse contains 1.1-compliant XML.

I'm using a DocumentBuilder to parse the XML (something like this):

public Element parseString(String xmlString) {
    try {
          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
          DocumentBuilder documentBuilder = dbf.newDocumentBuilder();

          InputSource source = new InputSource(new StringReader(xmlString));

      // Throws org.xml.sax.SAXParseException becuase of the invalid character refs
          Document doc = documentBuilder.parse(source);

          return doc.getDocumentElement();

    } catch (ParserConfigurationException pce) {
          // Handle the error
    } catch (SAXException se) {
          // Handle the error
    } catch (IOException ioe) {
          // Handle the error
    }
}

I've tried setting the XML header to indicate the XML conforms to the 1.1 spec...

xmlString = "<?xml version=\"1.1\" encoding=\"UTF-8\" ?>" + xmlString;

...but it is still parsed as 1.0 XML (still generates the invalid character reference exceptions).

How can I configure the Xerces parser to parse the XML as XML 1.1? Is there an alternative parser which provides better support for XML 1.1?

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

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

发布评论

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

评论(2

无法回应 2025-01-13 09:41:18

请参阅此处以获取 xerces 支持的所有功能的列表。可能您必须打开以下 2 个功能。

http://xml.org/sax/features/unicode-normalization-checking

True:执行 Unicode 规范化检查(如 XML 1.1 建议的第 2.13 节和附录 B 中所述)并报告规范化错误。

False:不报告 Unicode 规范化错误。

http://xml.org/sax/features/xml-1.1

True:解析器支持 XML 1.0 和 XML 1.1。
错误:解析器仅支持 XML 1.0。
访问权限:只读
自:Xerces-J 2.7.0
注意:此功能的值取决于 SAX 解析器拥有的解析器配置是否支持 XML 1.1。

See here for a list of all the features supported by xerces. May be below 2 features is what you have to turn on.

http://xml.org/sax/features/unicode-normalization-checking

True: Perform Unicode normalization checking (as described in section 2.13 and Appendix B of the XML 1.1 Recommendation) and report normalization errors.

False: Do not report Unicode normalization errors.

http://xml.org/sax/features/xml-1.1

True: The parser supports both XML 1.0 and XML 1.1.
False: The parser supports only XML 1.0.
Access: read-only
Since: Xerces-J 2.7.0
Note: The value of this feature will depend on whether the parser configuration owned by the SAX parser is known to support XML 1.1.

夜清冷一曲。 2025-01-13 09:41:18

不确定如何使用 Xerces 执行此操作,但 Woodstox 开箱即用地支持 XML 1.1。虽然它主要是一个 Stax 解析器,但它也实现了 SAX API(从版本 3.2 开始)。

Not sure how to do this with Xerces, but Woodstox supports XML 1.1 out of the box. While it is primarily a Stax parser, it also implements SAX API (since version 3.2).

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