Reputation: 6114
I have an XML that I would like to convert to a POJO using JAXB but I am not able to and all the elements within the parent class are getting populated as null
.
Input XML:
<Response>
<parameters>
<Id>101</Id>
<Status>SUCCESS</Status>
</parameters>
</Response>
FileWriterService.writeMethod()
JAXBContext context = JAXBContext.newInstance(Response.class);
Response response = (Response) context.createUnmarshaller().unmarshal(new
StringReader(inputXml));
System.out.println("response: " + response);
Output:
response: ClassPojo [parameters = ClassPojo [Id = null, Status = null]]
Response.java
@XmlRootElement(name="Response")
public class Response {
private Parameters parameters;
public Parameters getParameters() {
return parameters;
}
public void setParameters(Parameters parameters) {
this.parameters = parameters;
}
@Override
public String toString() {
return "ClassPojo [parameters = " + parameters + "]";
}
}
Parameters.java
public class Parameters {
private String Id;
private String Status;
public String getId() {
return Id;
}
public void setId(String Id) {
this.Id = Id;
}
public String getStatus() {
return Status;
}
public void setStatus(String Status) {
this.Status = Status;
}
@Override
public String toString() {
return "ClassPojo [Id = " + Id + ", Status = " + Status + "]";
}
}
Upvotes: 0
Views: 767
Reputation: 6114
Thanks for the pointer Lakshan. After implementing your solution the original problem got resolved but got error related com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions
In addition to your changes I had to add XmlAccessorType
as well:
@XmlRootElement(name = "Response")
@XmlAccessorType(XmlAccessType.FIELD)
public class Response {
Upvotes: 0
Reputation: 1436
Try with @XmlElement annotation, this annotation maps a field to an xml element
Response.java
@XmlElement
private Parameters parameters;
Parameters.java
@XmlElement
private String Id;
@XmlElement
private String Status;
Upvotes: 1