Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Note:</strong> I'm the <a href="http://www.eclipse.org/eclipselink/moxy.php" rel="nofollow"><strong>EclipseLink JAXB (MOXy)</strong></a> lead and a member of the <a href="http://jcp.org/en/jsr/detail?id=222" rel="nofollow"><strong>JAXB (JSR-222)</strong></a> expert group.</p> <p>Below is an example of how you could use MOXy's <code>@XmlNamedObjectGraphs</code> extension to map your use case.</p> <h2>Java Model</h2> <p><strong>Foo</strong></p> <p>The <code>@XmlNamedObjectGraphs</code> extension allows you to specify multiple subsets of mappings identified by a key.</p> <pre><code>import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement; import org.eclipse.persistence.oxm.annotations.XmlNamedAttributeNode; import org.eclipse.persistence.oxm.annotations.XmlNamedObjectGraph; import org.eclipse.persistence.oxm.annotations.XmlNamedObjectGraphs; @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) @XmlNamedObjectGraphs({ @XmlNamedObjectGraph( name="v1", attributeNodes = { @XmlNamedAttributeNode("field1"), @XmlNamedAttributeNode("field2")}), @XmlNamedObjectGraph( name="v2", attributeNodes = { @XmlNamedAttributeNode("field2"), @XmlNamedAttributeNode("field3")}) }) public class Foo { private String field1 = "ONE"; private String field2 = "TWO"; private String field3 = "THREE"; } </code></pre> <p><strong>jaxb.properties</strong></p> <p>To use MOXy as your JAXB provider you need to include a file called <code>jaxb.properties</code> with the following entry (see: <a href="http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html" rel="nofollow">http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html</a>).</p> <pre><code>javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory </code></pre> <h2>Demo Code</h2> <p><strong>Demo</strong></p> <p>You can specify the key corresponding to the object graph to have that subset applied to the object you are marshalling.</p> <pre><code>import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; import org.eclipse.persistence.jaxb.MarshallerProperties; public class Demo { public static void main(String[] args) throws Exception { JAXBContext jc = JAXBContext.newInstance(Foo.class); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); Foo foo = new Foo(); // Marshal Everything marshaller.marshal(foo, System.out); // Marshal "v1" Data marshaller.setProperty(MarshallerProperties.OBJECT_GRAPH, "v1"); marshaller.marshal(foo, System.out); // Marshal "v2" Data marshaller.setProperty(MarshallerProperties.OBJECT_GRAPH, "v2"); marshaller.marshal(foo, System.out); } } </code></pre> <p><strong>Output</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;foo&gt; &lt;field1&gt;ONE&lt;/field1&gt; &lt;field2&gt;TWO&lt;/field2&gt; &lt;field3&gt;THREE&lt;/field3&gt; &lt;/foo&gt; &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;foo&gt; &lt;field1&gt;ONE&lt;/field1&gt; &lt;field2&gt;TWO&lt;/field2&gt; &lt;/foo&gt; &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;foo&gt; &lt;field2&gt;TWO&lt;/field2&gt; &lt;field3&gt;THREE&lt;/field3&gt; &lt;/foo&gt; </code></pre> <h2>For More Information</h2> <ul> <li><a href="http://blog.bdoughan.com/2013/03/moxys-object-graphs-inputoutput-partial.html" rel="nofollow">http://blog.bdoughan.com/2013/03/moxys-object-graphs-inputoutput-partial.html</a></li> </ul>
    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