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

问题描述 投票:0回答:4

我有以下 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 中的

&amp;
更改为
&apos;
时,它起作用了。看起来问题只与符号
&amp;
有关,我不明白为什么。

解组的代码是:

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

有人知道吗?

编辑:我尝试了下面@abhin4v 建议的解决方案(即,在

&amp;
之后添加一个空格),但它似乎也不起作用。这是堆栈跟踪:

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)
java xml jaxb unmarshalling
4个回答
3
投票

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

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

HTH


3
投票

Xerces 将

&amp;
转换为
&
,然后尝试解析
&Address
但失败了,因为它没有以
;
结尾。
&
Address
之间放置一个空格,它应该可以工作。
放置一个空格将不起作用,因为 Xerces 现在将尝试解决
&
并抛出 OP 中给出的第二个错误。您可以将测试包装在 CDATA 部分中,Xerces 将不会尝试解析实体。


1
投票

原来是因为我用的框架(Mentawai framework)的问题。所述 XML 来自 HTTP 请求的 POST 正文。

显然,框架转换了XML主体中的字符实体,因此,

&amp;
变成了
&
,解组器无法解组XML。


0
投票

我发现添加 amp; 将修复解组错误。你希望它看起来像这样:

<address1>Test&amp;amp;Address</address1>

我认为这告诉解组器应该将 & 号作为数据值(在本例中为文本)而不是实体标识符来读取。您可以从您的错误中看到它正在尝试查看紧跟在

&amp;
之后的“地址”作为实体名称

© www.soinside.com 2019 - 2024. All rights reserved.