2013-01-25 35 views
3

我要解组简单的XML作为贝洛奥里藏特,但得到错误的意外元素的错误进行解

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 class:

WebResponse类,用于存储所检索的XML

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类:

Tester类

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

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

+1

我忽略了一点小感谢这布莱斯解决了我的异常 – user2010562

回答

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好点,容易错过 – Qwerky

+0

,但是我不能在解组后打印statuscode:0和description:null请帮助 – user2010562

+0

这就是我回答的第2部分和@Ash给出的答案是关于。我已经更新了我的答案,并希望能够像在代码中一样。 –

1

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

我认为,由于您的XML元素转到WebResponse - > Status - > StatusCode/Description,您的WebResponse类应该只有一个字段:Status类型的“状态”。 Status类应具有StatusCodeDescription字段。

编辑:另外,我认为@XmlElement注解去上的字段,而不是方法,但它已经有一段时间,因为我已经做到了......

+0

+1引入了'Status'这个类是没有什么原因造成例外情况,但需要正确解组数据。默认情况下,注释依赖于方法,如果你指定了'@XmlAccessorType(XmlAccessType.FIELD)',你可以在字段上设置它们:http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to .html –

+0

@Blaise,为更正而欢呼。 – Ash