Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <a href="https://stackoverflow.com/questions/6084895/xmlelements-marked-with-xmljavatypeadapters/6103442#6103442">answer given by bamana</a> is correct, however the exception you are seeing is due to a bug in the JAXB reference implementation. This bug also existed in <a href="http://www.eclipse.org/eclipselink/moxy.php" rel="nofollow noreferrer">EclipseLink JAXB (MOXy)</a> but has been fixed in the 2.3.0 stream, a nighty download can be obtained here:</p> <ul> <li><a href="http://www.eclipse.org/eclipselink/downloads/nightly.php" rel="nofollow noreferrer">http://www.eclipse.org/eclipselink/downloads/nightly.php</a></li> </ul> <p>As a workaround you could use the XmlAdapter approach that was in your original question:</p> <p><strong>SimpleAdapter</strong></p> <pre><code>import javax.xml.bind.annotation.adapters.XmlAdapter; public class SimpleAdapter extends XmlAdapter&lt;String, Simple&gt; { @Override public Simple unmarshal(String v) throws Exception { Simple simple = new Simple(); simple.setSimple(v); return simple; } @Override public String marshal(Simple v) throws Exception { return v.getSimple(); } } </code></pre> <p><strong>Simple</strong></p> <pre><code>import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; @XmlJavaTypeAdapter(SimpleAdapter.class) public class Simple extends Value { private java.lang.String simple; public java.lang.String getSimple() { return simple; } public void setSimple(java.lang.String simple) { this.simple = simple; } } </code></pre> <p><strong>Compound</strong></p> <pre><code>import java.util.List; import javax.xml.bind.annotation.*; @XmlRootElement(name = "compound") @XmlAccessorType(XmlAccessType.FIELD) public class Compound extends Value { @XmlElements({ @XmlElement(name = "simple", type = Simple.class), @XmlElement(name = "compound", type = Compound.class) }) protected List&lt;Value&gt; value; public List&lt;Value&gt; getValue() { return value; } public void setValue(List&lt;Value&gt; value) { this.value = value; } } </code></pre> <p><strong>Value</strong></p> <pre><code>import java.io.Serializable; public abstract class Value implements Serializable {} </code></pre> <p><strong>Demo</strong></p> <pre><code>import java.io.File; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; public class Demo { public static void main(String[] args) throws Exception { JAXBContext jc = JAXBContext.newInstance(Compound.class); Unmarshaller unmarshaller = jc.createUnmarshaller(); Compound compound = (Compound) unmarshaller.unmarshal(new File("input.xml")); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(compound, System.out); } } </code></pre> <p><strong>input.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt; &lt;compound&gt; &lt;simple&gt; &lt;simple&gt;FOO&lt;/simple&gt; &lt;/simple&gt; &lt;compound/&gt; &lt;/compound&gt; </code></pre>
    singulars
    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. 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