Thursday, 8 August 2013

Parsing Solr status response XML with JAXB

Parsing Solr status response XML with JAXB

Solr returns status information in the following XML form (this is a
shortened version):
<?xml version="1.0" encoding="UTF-8"?>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">2</int>
</lst>
</response>
I'd like to pull this into my Java application. I created the following code:
@XmlRootElement(name="response")
@XmlAccessorType(XmlAccessType.FIELD)
public class Response {
@XmlElement(name="lst")
private ResponseHeader responseHeader;
public ResponseHeader getResponseHeader() {
return responseHeader;
}
public void setResponseHeader(ResponseHeader responseHeader) {
this.responseHeader = responseHeader;
}
public Response() {}
}
for the root element, and
@XmlAccessorType(XmlAccessType.FIELD)
public class ResponseHeader {
@XmlElement(name="int")
private IntegerElement status;
@XmlElement(name="int")
private IntegerElement QTime;
....
}
for the code inside. Finally, there's this for the int fields:
@XmlAccessorType(XmlAccessType.FIELD)
public class IntegerElement implements Serializable {
@XmlTransient
private static final long serialVersionUID = 1L;
@XmlAttribute
protected String name;
@XmlValue
private int value;
...
}
When I try to unmarshal the piece of xml above, only one of the int
elements is filled. The other doesn't get set (i.e. null). No errors
though. How should I go about annotating these classes better?

No comments:

Post a Comment