Java XML解组在使用JAXB的ampersand(&;amp;)上失败
我有以下XML:Java XML解组在使用JAXB的ampersand(&;amp;)上失败,java,xml,jaxb,unmarshalling,Java,Xml,Jaxb,Unmarshalling,我有以下XML: <?xml version="1.0" encoding="UTF-8"?> <details> ... <address1>Test&Address</address1> ... </details> 但是当我改变了&转换为&apos,它可以工作。看起来问题只存在于“与”符号&
<?xml version="1.0" encoding="UTF-8"?>
<details>
...
<address1>Test&Address</address1>
...
</details>
但是当我改变了&将XML中的code>转换为&apos代码>,它可以工作。看起来问题只存在于“与”符号&我不明白为什么
要解组的代码是:
JAXBContext context = JAXBContext.newInstance("some.package.name", this.getClass().getClassLoader());
Unmarshaller unmarshaller = context.createUnmarshaller();
obj = unmarshaller.unmarshal(new StringReader(xml));
有人有什么见解吗
编辑:我尝试了下面@abhin4v建议的解决方案(即在和之后添加一个空格),但似乎不起作用。这是stacktrace:
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)
Xerces转换&
到&
,然后尝试解决&地址
,但失败,因为它没有以结尾代码>。在&
和地址
之间留一个空格,它应该可以工作。放置空格将不起作用,因为Xerces现在将尝试解析&
,并抛出OP中给出的第二个错误。您可以将测试包装在CDATA部分,Xerces将不会尝试解析实体。我也遇到过这种情况。首先,我简单地将&替换为一个令牌字符串(AMPERSAND_-token),通过JAXB发送它,然后重新替换AMPERSAND。不理想,但这是一个快速修复
第二关我做了很多重大的改变,所以我不确定到底是什么解决了这个问题。我怀疑提供对HTMLDTD的JAXB访问会让它更快乐,但这只是一个猜测,可能是我的项目特有的
HTH事实证明,问题在于我使用的框架()。所述XML来自HTTP请求的POST主体
显然,框架转换XML正文中的字符实体,因此,&
变成&
,而解组器无法解组XML。看起来像个bug,您当然可以报告它。作为一种选择,您可以只使用特殊符号,而不关心特殊符号。JRE 1.6.0_20没有这个问题。可能已经修复了。@axtavt:你能给我指一下证实这一点的文档(可能是发行说明吗?)吗?我刚刚在JDK1.6.0_16下写了一个快速单元测试,也在那里工作。您使用的是哪个版本的JDK和/或JAXB?我会尝试使用sun内部xml解析器,而不是您使用的任何xerces实现。呃,这是一件非常愚蠢的事情。并不能完全激发对框架其余部分的信心。是的,不幸的是,使用这个特定框架是公司的选择。我只能抱怨:(这与所使用的框架毫无关系。明打威不在HTTP级别执行任何类型的o转换。它按原样传递POST参数。
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)