2010-11-07 45 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