Unexpected element error during disassembly
I have to reverse the simple xml as belo but get error like
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.
This xml is present in the response.xml file
<?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>
Below is my code:
WebResponse class:
Webresponse class to store the retrieved 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 class:
Tester class
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.*;
I have used the solutions present on this site but could not solve it, please help
source to share
Part 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)
The problem is the namespaces specified in the XML ( response/v1
and request/v1
).
<WebResponse xmlns="http://example.com/service/response/v1">
and package-info
differ
@XmlSchema(namespace = "http://example.com/service/request/v1", elementFormDefault = XmlNsForm.QUALIFIED)
package com.example;
Part 2
Your current object model and mappings do not match the XML content. One way to solve this problem would be to represent the class Status
as Ash answered .
Status
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;
}
}
source to share
Try to add a class Status
and put it between the WebResponse
and fields.
I think that since your XML elements go to WebResponse -> Status -> StatusCode / Description, your class WebResponse
should only have one field: "status" type Status
. The class Status
must have fields StatusCode
and Description
.
Edit: Also, I thought @XmlElement annotations go by fields, not methods, but it's been a while since I did this ...
source to share