Note that there are some explanatory texts on larger screens.

plurals
  1. POxmlElements with same name and different types
    text
    copied!<p>) </p> <p>I have an xmlelement which can be from different Types. Independent of the type it has the same name. It can be an object or just a reference via URI to an existing object. I thought xmlElements could be the solution. Marshalling works fine but by unmarshalling it, it chooses everytime the last given class type.</p> <p>The Class Flower which contains the Element</p> <pre><code>@XmlRootElement(name = "Flower") @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "id", "name", "refName", "description", "created", "updated", "color", "seed") public class Flower extends CommonElements{ private string color; @XmlElements({ @XmlElement(name="seed", type=Seed.class), @XmlElement(name="seed", type=Reference.class) }) public Object seed; } </code></pre> <p>The class seed which is one of the Types the element can contain</p> <pre><code>@XmlRootElement(name = "Seed") @XmlAccessorType(XmlAccessType.FIELD) @XmlType(propOrder = { "id", "name", "refName", "description", "created", "updated", "category", "country"}) public class Seed extends CommonElements{ protected String category = ""; protected String country = ""; } </code></pre> <p>And the other class which can be contained by the element </p> <pre><code>@XmlRootElement @XmlAccessorType(XmlAccessType.NONE) public class Reference { @XmlAttribute(name="href") protected URI href; } </code></pre> <p>In CommonElements are just some generic elements like id, refName, description etc.. The XML can look like</p> <pre><code>&lt;Flower&gt; &lt;id&gt;http://localhost/test/flowers/1&lt;/id&gt; &lt;refName&gt;redRose&lt;/refName&gt; &lt;description&gt;classical red rose &lt;/description&gt; &lt;color&gt;red&lt;/color&gt; &lt;seed href="http://localhost/test/seeds/1" /&gt; &lt;/Flower&gt; </code></pre> <p>Or like</p> <pre><code>&lt;Flower&gt; &lt;id&gt;http://localhost/test/flowers/1&lt;/id&gt; &lt;refName&gt;redRose&lt;/refName&gt; &lt;description&gt;classical red rose &lt;/description&gt; &lt;color&gt;red&lt;/color&gt; &lt;seed&gt; &lt;id&gt;http://localhost/test/seeds/1&lt;/id&gt; &lt;refName&gt;wildrose&lt;/refName&gt; &lt;description&gt;Special Seed for beautiful wild roses&lt;/description&gt; &lt;category&gt;wildrose&lt;/category&gt; &lt;country&gt;china&lt;/country&gt; &lt;/seed&gt; &lt;/Flower&gt; </code></pre> <p>I assumed that the different structure of the classes are enough for jaxb to distinguish the objects. I am afraid I have to use adapters but I hope someone has another great idea.</p> <p>I know there is one Topic similiar to mine. But the types in the topic looks alike so jaxb can't distinguish them. (<a href="https://stackoverflow.com/questions/5007516/jaxb-xmlelements-different-types-but-same-name">JAXB @XmlElements, different types but same name?</a>)</p> <p>Thanks and sorry for my bad english.</p> <p>Edit: Is there a way to add the type by marshalling, that jaxb knows exactly which type it is for unmarshalling? </p>
 

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