2013-01-25 57 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