问题解组xml doc到Java对象(jaxb)

时间:2010-11-07 16:22:22

标签: xml jaxb xml-namespaces unmarshalling

我使用xjc从XSD创建java对象。

现在我正在尝试将xml doc解组为java对象,但我得到了:

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

这里有什么吗?

编辑:

我正在传递一个org.w3c.dom.Document对象,它从Web服务调用(轴Web服务)返回...

注意,从这里要解析的ws返回的Document对象包含以下根元素:

<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;

1 个答案:

答案 0 :(得分: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。