Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you should use inheritance instead of generics. Given an XML like this:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt; &lt;id&gt;000000&lt;/id&gt; &lt;results&gt; &lt;result xsi:type="X1Result"&gt; &lt;year&gt;2003&lt;/year&gt; &lt;title&gt;Lorem Ipsum&lt;/title&gt; &lt;items&gt; &lt;item&gt;I1&lt;/item&gt; &lt;item&gt;I2&lt;/item&gt; &lt;/items&gt; &lt;/result&gt; &lt;result xsi:type="X1Result"&gt; &lt;year&gt;2007&lt;/year&gt; &lt;title&gt;Dolor sit amet&lt;/title&gt; &lt;items&gt; &lt;item&gt;K1&lt;/item&gt; &lt;item&gt;K2&lt;/item&gt; &lt;/items&gt; &lt;/result&gt; &lt;/results&gt; &lt;/response&gt; </code></pre> <p>You can dynamically bind your <code>&lt;result&gt;</code> entries. You have a top-level type:</p> <pre><code>@XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "XResult") @XmlSeeAlso({ X1Result.class })public abstract class XResult { } </code></pre> <p>And you have implementing classes:</p> <pre><code>@XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "X1Result") public class X1Result extends XResult { @XmlElement(name = "year") private String year; @XmlElement(name = "title") private String title; @XmlElementWrapper(name = "items") @XmlElement(name = "item") private List&lt;String&gt; items; ... } </code></pre> <p>Use the top-level type in your XResponse class:</p> <pre><code>@XmlRootElement(name = "response") @XmlAccessorType(XmlAccessType.FIELD) public class XResponse { @XmlElement(name = "id") private String id; @XmlElementWrapper(name = "results") @XmlElement(name = "result") private List&lt;XResult&gt; results; ... } </code></pre> <p>And you can unmarshall using the top-level type:</p> <pre><code>context = JAXBContext.newInstance(XResponse.class, XResult.class); Unmarshaller unmarshaller = context.createUnmarshaller(); XResponse response = (XResponse) unmarshaller.unmarshal(new File("testfile.xml")); List&lt;XResult&gt; results = response.getResults(); for (XResult object : results) { System.out.println(object.getClass()); } </code></pre>
    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.
    1. VO
      singulars
      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