2013-05-04 62 views
1

我正在測試MOXy 2.5.0 RC1。元帥行爲差異

我編組以下字符串:

<c r="C3" xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"> 
    <v>20</v> 
    </c> 

它由https://github.com/plutext/docx4j/blob/master/src/xlsx4j/java/org/xlsx4j/sml/Cell.java

通知不存在任何@XmlRootElement註釋

隨着參考實施方式中,結果的表示,如所預期,是:

javax.xml.bind.MarshalException 
- with linked exception: 
[com.sun.istack.internal.SAXException2: unable to marshal type "org.xlsx4j.sml.Cell" as an element because it is missing an @XmlRootElement annotation] 
     at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:317) 
     at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:243) 
     at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:96) 
     at org.docx4j.XmlUtils.marshaltoString(XmlUtils.java:507) 

With MOXy,結果是:

<v>20</v> 

這是已知問題嗎?我沒有試過2.4.2 RC1。

感謝..

回答

1

EclipseLink MOXy和RI之間的已知差異。我們已經在MOXy中打開了這個門,用於編組爲OutputStreamWriter,其中根元素已被寫入。

你是否指望拋出異常。如果沒有根元素,則可以將對象包裝在JAXBElement的實例中。

解決方法

可以使用JAXBIntrospector來確定對象是否具有根元素。

JAXBIntrospector introspector = jaxbContext.createJAXBIntrospector(); 
QName rootElement = introspector.getElementName(aPOJO); 
if(null == rootElement) { 
    // ... 
} else { 
    // ... 
} 
+0

感謝您的回答。我會思考其中的含義。目前,它比任何事情都更令人驚訝...如果包含2個孩子,並且用戶正在編組DOM節點,那麼會發生什麼? – JasonPlutext 2013-05-05 03:06:03