Note that there are some explanatory texts on larger screens.

plurals
  1. POParsing Solr status response XML with JAXB
    primarykey
    data
    text
    <p>Solr returns status information in the following XML form (this is a shortened version):</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;response&gt; &lt;lst name="responseHeader"&gt; &lt;int name="status"&gt;0&lt;/int&gt; &lt;int name="QTime"&gt;2&lt;/int&gt; &lt;/lst&gt; &lt;/response&gt; </code></pre> <p>I'd like to pull this into my Java application. I created the following code:</p> <pre><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() {} } </code></pre> <p>for the root element, and </p> <pre><code>@XmlAccessorType(XmlAccessType.FIELD) public class ResponseHeader { @XmlElement(name="int") private IntegerElement status; @XmlElement(name="int") private IntegerElement QTime; .... } </code></pre> <p>for the code inside. Finally, there's this for the int fields:</p> <pre><code>@XmlAccessorType(XmlAccessType.FIELD) public class IntegerElement implements Serializable { @XmlTransient private static final long serialVersionUID = 1L; @XmlAttribute protected String name; @XmlValue private int value; ... } </code></pre> <p>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?</p> <p>A longer piece of the XML is this:</p> <pre><code> &lt;lst name="index"&gt; &lt;int name="numDocs"&gt;0&lt;/int&gt; &lt;int name="maxDoc"&gt;0&lt;/int&gt; &lt;int name="deletedDocs"&gt;0&lt;/int&gt; &lt;long name="version"&gt;1&lt;/long&gt; &lt;int name="segmentCount"&gt;0&lt;/int&gt; &lt;bool name="current"&gt;true&lt;/bool&gt; &lt;bool name="hasDeletions"&gt;false&lt;/bool&gt; &lt;str name="directory"&gt;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)&lt;/str&gt; &lt;lst name="userData" /&gt; &lt;long name="sizeInBytes"&gt;65&lt;/long&gt; &lt;str name="size"&gt;65 bytes&lt;/str&gt; &lt;/lst&gt; </code></pre> <p>I've adapted Benoit Wickramarachi sample with inheritance to produce this: </p> <pre><code>@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&lt;ResponseElement&gt; index; public Index() {} </code></pre> <p>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).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload