Note that there are some explanatory texts on larger screens.

plurals
  1. POJAXB - Set an XmlAdapter only for an specific sub type of an element?
    text
    copied!<p>I have to generate a xml element that can have as value any "primitive type" (xsd:string, xsd:boolean, etc). Examples:</p> <pre><code>&lt;field xsi:type="xsd:string" name="aString"&gt;String Value&lt;/field&gt; &lt;field xsi:type="xsd:dateTime" name="aDate"&gt;2011-10-21&lt;/field&gt; &lt;field xsi:type="xsd:dateTime" name="aDateTime"&gt;2011-10-21T12:00:00&lt;/field&gt; ... </code></pre> <p>So, I use this implementation that makes JAXB decide the <code>xsi:type</code> of the primitive type:</p> <pre><code>public class Field { @XmlAttribute private String name; @XmlElement Object value; } </code></pre> <p>and it is working as expected but all the java.util.Date gets the type <code>xs:dateTime</code> ... </p> <p>Now I want to change the behavior of the marshaller ONLY when the 'value' object is an instance of java.util.Date to obtains fields like this:</p> <pre><code>&lt;field xsi:type="xsd:date" name="aDate"&gt;2011-10-21&lt;/field&gt; &lt;field xsi:type="xsd:dateTime" name="aDateTime"&gt;2011-10-21T12:00:00&lt;/field&gt; </code></pre> <p>So I create an adapter, but if I try this:</p> <pre><code>@XmlElement @XmlJavaTypeAdapter(DateAdapter.class) Object value; </code></pre> <p>The adapter must handles a java.lang.Object type</p> <pre><code>public class DateAdapter extends XmlAdapter&lt;String, Object&gt; {...} </code></pre> <p>But I do not want to loose the JAXB marshallers for all the others objects (Integer, Double, etc) ... </p> <p>there is a way to set an adapter for a specific subtype of an element?</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