我正在尝试解析包含符合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 提供更好的支持?