2010-11-07 47 views
1

我已經使用xjc從XSD創建Java對象。unmarshalling xml文檔到Java對象(jaxb)問題

,現在我試圖將XML文檔解組到Java對象,但我得到:

javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"GlobalComponentInformation

任何這裏嗎?

編輯:

我傳遞一個org.w3c.dom.Document中的對象,它從一個Web服務調用(Axis Web服務)返回......

注意,Document對象從WS返回這裏要分析的包含以下根元素:

<GlobalInformationResponseDocument xmlns="" /> 

@XmlRootElement類的樣子:

XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
    "wsExternalResponse" 
}) 
@XmlRootElement(name = "GlobalInformationResponseDocument") 
public class GlobalInformationResponseDocument { 

    @XmlElement(name = "WS_ExternalResponse", required = true) 
    protected WSExternalResponseType wsExternalResponse; 

    /** 
    * Gets the value of the wsExternalResponse property. 
    * 
    * @return 
    *  possible object is 
    *  {@link WSExternalResponseType } 
    *  
    */ 
    public WSExternalResponseType getWSExternalResponse() { 
     return wsExternalResponse; 
    } 

    /** 
    * Sets the value of the wsExternalResponse property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link WSExternalResponseType } 
    *  
    */ 
    public void setWSExternalResponse(WSExternalResponseType value) { 
     this.wsExternalResponse = value; 
    } 

} 

包信息:

@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.mycompany.com/GlobalInformationResponseExt", 
     elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED) 
package com.company.jaxb.client; 
+0

你的根類的@XmlRootElement是什麼樣的?另外你的包信息類是什麼樣的(如果你有)?還有什麼樣的XML輸入是你解組(流,DOM等)? – 2010-11-08 03:57:04

+0

GlobalComponentInformation在哪裏進來?它可能是在你分析的XML響應中返回的?你沒有在XSD中定義它嗎? – Kissaki 2010-11-08 10:56:10

回答

1

您是從Web服務類接收的根元素:

<GlobalInformationResponseDocument xmlns="" /> 

不匹配,是根據你的JAXB映射的預期期望根元素:

<GlobalInformationResponseDocument xmlns="http://www.mycompany.com/GlobalInformationResponseExt" /> 

package-info類指定所有元素應該是名稱空間限定的(javax.xml.bind.annotation.XmlNsForm.QUALIFIED),並且默認名稱空間是 「http://www.mycompany.com/GlobalInformationResponseExt」

@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.mycompany.com/GlobalInformationResponseExt", 
     elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED) 
package com.company.jaxb.client; 

你要麼需要修復的XML文檔,或更改JAXB映射匹配documnent。在這種情況下,通過刪除package-info。

+0

我確實更改了xml文檔,然後傳遞給JAXB編組對象,該對象不接受修改的對象,除非我將該文檔轉換爲字符串,然後轉換爲doc! – 2010-11-09 12:32:32

+0

當你「更改xml文檔」時你做了什麼?自從將文檔轉換爲字符串並返回到文檔後,我假設您的更改沒有正確解決名稱空間不匹配問題。 – 2010-11-09 14:03:28