2010-06-08 61 views
2

號(&)失敗,我有以下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

看起來像一個錯誤,你當然可以報告它。作爲一個選項,您可以堅持使用<![CDATA [...]]>而不關心特殊符號。 – 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

回答

1

事實證明,這個問題是因爲我使用的框架(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

3

的Xerces轉換&amp;&,然後嘗試解決&Address,因爲它不符合;結束其失敗。 請在 &Address之間加一個空格,它應該可以工作。 因爲Xerces現在會嘗試解決&並拋出OP中給出的第二個錯誤,所以放置一個空格將不起作用。您可以將測試包裝在CDATA部分,Xerces不會嘗試解析實體。

+0

不工作:( 看到我在編輯中的問題 – ryanprayogo 2010-06-08 16:34:48

3

我也遇到過。第一遍,我簡單地將&放大器替換爲令牌字符串(AMPERSAND_TOKEN),通過JAXB發送它,然後重新替換&符號。不理想,但它是一個快速修復。

第二遍我做了很多重大更改,所以我不確定究竟是什麼解決了問題。我懷疑提供JAXB訪問html dtds使它更快樂,但這只是一個猜測,可能是我的項目特定的。

HTH