Marceau
Marceau

Reputation: 1703

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?

A longer piece of the XML is this:

  <lst name="index">
    <int name="numDocs">0</int>
    <int name="maxDoc">0</int>
    <int name="deletedDocs">0</int>
    <long name="version">1</long>
    <int name="segmentCount">0</int>
    <bool name="current">true</bool>
    <bool name="hasDeletions">false</bool>
    <str name="directory">org.apache.lucene.store.NRTCachingDirectory:NRTCachingDirectory(org.apache.lucene.store.MMapDirectory@/home/marceau/Developer/Servers/solr/example/solr/collection1/data/index lockFactory=org.apache.lucene.store.NativeFSLockFactory@22bda240; maxCacheMB=48.0 maxMergeSizeMB=4.0)</str>
    <lst name="userData" />
    <long name="sizeInBytes">65</long>
    <str name="size">65 bytes</str>
  </lst>

I've adapted Benoit Wickramarachi sample with inheritance to produce this:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="lst")
public class Index {

@XmlElementWrapper( name = "lst" )
@XmlElements( { 
    @XmlElement( name="int", type = IntegerElement.class ),
    @XmlElement( name="str", type = StringElement.class),
    @XmlElement( name="bool", type = BooleanElement.class),
    @XmlElement( name="date", type = DateElement.class),
    @XmlElement( name="long", type = LongElement.class),
    @XmlElement( name="lst", type = ListElement.class)
    } )
private List<ResponseElement> index;

public Index() {}

The unmarshalling process doesn't crash, but it merely creates an Index object containing an ArrayList named index that is completely empty (10 nulls in debugger, size 0).

Upvotes: 2

Views: 983

Answers (2)

Benoit Wickramarachi
Benoit Wickramarachi

Reputation: 6226

Change the lines:

@XmlElement(name="int")
private IntegerElement status;

@XmlElement(name="int")
private IntegerElement QTime;

to:

@XmlElement(name="int")
private List<IntegerElement> intElements;

So that all int element would be unmarshalled in a List.

Then to get values iterate over the list:

for(IntegerElement intEl : intElements){
    System.out.println(intEl.getName() + " = " + intEl.getValue());
}

Edit:

If you want to use Inheritance in Jaxb, use the following annotations (IElement behing the interface implemented by IntegerElement and StringElement):

@XmlElementWrapper( name = "lst" )
@XmlElements( { 
    @XmlElement( name="int", type = IntegerElement.class ),
    @XmlElement( name="str", type = StringElement.class)
    } )
private List<IElement> elements;

Upvotes: 1

bdoughan
bdoughan

Reputation: 149057

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

You could use MOXy's @XmlPath extension to support this use case:

import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath

@XmlAccessorType(XmlAccessType.FIELD)
public class ResponseHeader {

    @XmlPath("int[1]")
    private IntegerElement status;

    @XmlPath("int[2]")
    private IntegerElement QTime;

    ....

}

For More Information

Upvotes: 2

Related Questions