我要解組簡單的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.*;
我使用了本網站的解決方案,但無法解決請幫助
我忽略了一點小感謝這布萊斯解決了我的異常 – user2010562