解组时出现意外的元素错误

时间:2013-01-25 10:29:17

标签: java jaxb unmarshalling

我应该将简单的xml解组为belo,但是将错误视为

Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"http://example.com/service/response/v1", local:"WebResponse"). Expected elements are <{http://example.com/service/request/v1}WebResponse>
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:603)

I tried the solutions in this site but could not solve pls help.

这是response.xml文件中的xml

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
- <WebResponse xmlns="http://example.com/service/response/v1">
- <Status>
  <StatusCode>0</StatusCode> 
  <Description>Transaction was successful</Description> 
  </Status>
  </WebResponse>

以下是我的代码:

WebResponse类:

用于存储检索到的xml的webresponse类

import javax.xml.bind.annotation.*;

@XmlRootElement(name="WebResponse")
public class WebResponse {

    private long statusCode;
    private String description;
    @XmlElement(name= "StatusCode")
    public long getStatusCode() {
        return statusCode;
    }
    public void setStatusCode(long statusCode) {
        this.statusCode = statusCode;
    }
    @XmlElement(name= "Description")
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }

WebResponseMain类:

测试者课程

import javax.xml.bind.Unmarshaller;
import javax.xml.bind.JAXBContext;

import java.io.FileReader;
import com.example.WebResponse;


public class WebResponseMain {

    public static void main(String[] args) throws Exception{
        JAXBContext context = JAXBContext.newInstance(WebResponse.class);
        Unmarshaller um = context.createUnmarshaller();
        WebResponse WR = (WebResponse) um.unmarshal(new FileReader("c:/tem/Response.XML"));
        System.out.println("StatusCode: " + WR.getStatusCode() + " Description "
                 +WR.getDescription());

    }

}

package-info.java:

@XmlSchema(namespace = "http://example.com/service/request/v1",elementFormDefault=XmlNsForm.QUALIFIED)



package com.example;

import javax.xml.bind.annotation.*;

我使用了本网站提供的解决方案但无法解决请求帮助

2 个答案:

答案 0 :(得分:3)

第1部分

Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"http://example.com/service/response/v1", local:"WebResponse"). Expected elements are <{http://example.com/service/request/v1}WebResponse>
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:603)

问题是XML中指定的命名空间(response/v1request/v1)。

<WebResponse xmlns="http://example.com/service/response/v1">

package-info不同

@XmlSchema(namespace = "http://example.com/service/request/v1", elementFormDefault = XmlNsForm.QUALIFIED)
package com.example;

第2部分

您当前的对象模型和映射与XML内容不匹配。解决此问题的一种方法是引入Statusas answered by Ash

状态

import javax.xml.bind.annotation.XmlElement;

public class Status {

    private long statusCode;
    private String description;

    @XmlElement(name = "StatusCode")
    public long getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(long statusCode) {
        this.statusCode = statusCode;
    }

    @XmlElement(name = "Description")
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

}

<强> WebResponse的

import javax.xml.bind.annotation.*;

@XmlRootElement(name = "WebResponse")
public class WebResponse {

    private Status status;

    @XmlElement(name="Status")
    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

}

答案 1 :(得分:1)

尝试添加Status课程,并将其放在WebResponse和字段之间。

我认为,因为你的XML元素是WebResponse - &gt;状态 - &gt; StatusCode / Description,您的WebResponse类应该只有一个字段:类型为Status的“状态”。然后,班级Status应该包含StatusCodeDescription字段。

编辑:另外,我认为@XmlElement注释是在字段上进行的,而不是方法,但是我已经做了一段时间了......