Java XML 解码在使用 JAXB 的符号 (&) 上失败

标签 java xml jaxb unmarshalling

我有以下 XML:

<?xml version="1.0" encoding="UTF-8"?>
<details>
  ...
  <address1>Test&amp;Address</address1>
  ...
</details>

当我尝试使用 JAXB 解码它时,它抛出以下异常:

Caused by: org.xml.sax.SAXParseException: The reference to entity "Address" must end with the ';' delimiter.
        at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
        at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)
        at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
        at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
        at org.apache.xerces.impl.XMLScanner.reportFatalError(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEntityReference(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
        at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
        at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
        at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
        at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:194)

但是当我将 XML 中的 & 更改为 ' 时,它起作用了。看起来问题只出在与符号 & 上,我不明白为什么。

要解码的代码是:

JAXBContext context = JAXBContext.newInstance("some.package.name", this.getClass().getClassLoader());
Unmarshaller unmarshaller = context.createUnmarshaller();
obj = unmarshaller.unmarshal(new StringReader(xml));

有人知道吗?

编辑:我尝试了下面@abhin4v 建议的解决方案(即,在 & 之后添加一个空格),但它似乎也不起作用。这是堆栈跟踪:

Caused by: org.xml.sax.SAXParseException: The entity name must immediately follow the '&' in the entity reference.
        at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
        at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)
        at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
        at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
        at org.apache.xerces.impl.XMLScanner.reportFatalError(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEntityReference(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
        at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
        at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
        at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
        at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:194)

最佳答案

我也遇到过。首先,我只是将 & 替换为标记字符串 (AMPERSAND_TOKEN),通过 JAXB 发送它,然后重新替换 & 符号。不理想,但这是一个快速修复。

第二遍我做了很多重要的改变,所以我不确定究竟是什么解决了问题。我怀疑提供对 html dtds 的 JAXB 访问会让它更快乐,但这只是一个猜测,可能特定于我的项目。

HTH

关于Java XML 解码在使用 JAXB 的符号 (&) 上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2999132/

相关文章:

java - 如何检查 ArrayList<ArrayList<String>> 是否包含某个 ArrayList<String>

java - SwingUtilities.InvokeLater 和 ButtonListeners

java - 获取 XSD 验证失败的元素的对象

xml - jaxb 不会为相同类型的不同元素生成类

java - Maven 在@XmlAccessorType 上抛出编译错误 "Cannot find symbol",但我可以找到它

java - JPanel 添加背景

java - 无法使用java中的扫描器扫描字符串数组的所有元素

c# - 使用Powershell遍历xml元素

java - 如何命名xs :elements in the generated wsdl?

java - 如何解码与 Java 的 JAXB 库的非层次关系?