Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could use a <code>@XmlAnyElement(lax=true)</code> and an <code>XmlAdapter</code> to handle this use case:</p> <p><strong>ServiceRq</strong></p> <pre><code>import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "serviceRq") public class ServiceRq{ @XmlJavaTypeAdapter(value=BodyAdapter.class) private Object body; // getters and setters omitted } </code></pre> <p><strong>BodyAdapter</strong></p> <pre><code>import javax.xml.bind.annotation.adapters.XmlAdapter; public class BodyAdapter extends XmlAdapter&lt;Body, Object&gt;{ @Override public Object unmarshal(Body v) throws Exception { return v.getValue(); } @Override public Body marshal(Object v) throws Exception { Body body = new Body(); body.setValue(v); return body; } } </code></pre> <p><strong>Body</strong></p> <pre><code>import javax.xml.bind.annotation.XmlAnyElement; public class Body { private Object value; @XmlAnyElement(lax=true) public Object getValue() { return value; } public void setValue(Object value) { this.value = value; } } </code></pre> <p><strong>CreateRq</strong></p> <pre><code>import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "createRq") public class CreateRq{ private String id; // getters and setters omitted } </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(ServiceRq.class); System.out.println(jc); Unmarshaller unmarshaller = jc.createUnmarshaller(); ServiceRq serviceRq = (ServiceRq) unmarshaller.unmarshal(new File("input.xml")); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(serviceRq, System.out); } } </code></pre> <p><strong>For More Information</strong></p> <ul> <li><a href="http://bdoughan.blogspot.com/2010/08/using-xmlanyelement-to-build-generic.html" rel="noreferrer">http://bdoughan.blogspot.com/2010/08/using-xmlanyelement-to-build-generic.html</a></li> <li><a href="http://bdoughan.blogspot.com/2010/07/xmladapter-jaxbs-secret-weapon.html" rel="noreferrer">http://bdoughan.blogspot.com/2010/07/xmladapter-jaxbs-secret-weapon.html</a></li> <li><a href="http://bdoughan.blogspot.com/2010/12/jaxb-and-immutable-objects.html" rel="noreferrer">http://bdoughan.blogspot.com/2010/12/jaxb-and-immutable-objects.html</a></li> <li><a href="http://bdoughan.blogspot.com/2010/12/represent-string-values-as-element.html" rel="noreferrer">http://bdoughan.blogspot.com/2010/12/represent-string-values-as-element.html</a></li> </ul>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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