Java的XML解组使用JAXB

问题描述:

号(&)失败,我有以下XML:Java的XML解组使用JAXB

<?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) 
+1

看起来像一个错误,你当然可以报告它。作为一个选项,您可以坚持使用而不关心特殊符号。 – 2010-06-08 16:27:11

+0

JRE 1.6.0_20没有这个问题。可能它已经修复了。 – axtavt 2010-06-08 16:34:00

+0

@axtavt:你能指出我的任何文件(发布说明,也许?),证实了这一点? – ryanprayogo 2010-06-08 16:38:39

事实证明,这个问题是因为我使用的框架(Mentawai framework)的。所述XML来自HTTP请求的POST正文。

显然,框架会转换XML正文中的字符实体,因此,&amp;将变为&,并且解组器无法解组XML。

+4

Urgh,这是一个非常愚蠢的事情,它并没有真正激发对框架的其余部分的信心 – skaffman 2010-06-08 19:42:06

+1

是的,不幸的是它的选择该公司使用这个特定的框架。我只能抱怨:( – ryanprayogo 2010-06-09 18:37:19

+1

这与使用的框架完全没有关系,Mentawai不会在HTTP级别执行任何类型的转换,它会按原样传递POST参数。 – TraderJoeChicago 2010-08-17 22:36:06

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

+0

不工作:( 看到我在编辑中的问题 – ryanprayogo 2010-06-08 16:34:48

我也遇到过。第一遍,我简单地将&放大器替换为令牌字符串(AMPERSAND_TOKEN),通过JAXB发送它,然后重新替换&符号。不理想,但它是一个快速修复。

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

HTH