Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<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 2 (JSR-222)</strong></a> expert group.</p> <blockquote> <p>Blaise : I have not used EclipseLink before, however I think I can move out of castor if it is relatively simple to adopt EclipseLink ? Its just that the entire project has been using Castor so it would be more consistent. However, could you please elaborate on Eclipselink. Thanks. – TYS</p> </blockquote> <p>EclipseLink MOXy is a JAXB (JSR-222) implementation. Looking at your question, this model can be mapped to the desired XML using any JAXB implementation as follows:</p> <p><strong>ClassA</strong></p> <pre><code>package forum9994762; import javax.xml.bind.annotation.*; @XmlRootElement(name="Sample") @XmlAccessorType(XmlAccessType.FIELD) public class ClassA { @XmlElement(name="data") private ClassB var1; @XmlAttribute private String var2; } </code></pre> <p><strong>ClassB</strong></p> <pre><code>package forum9994762; import javax.xml.bind.annotation.*; @XmlAccessorType(XmlAccessType.FIELD) public class ClassB { @XmlAttribute(name="var4") private ClassC var3; } </code></pre> <p><strong>ClassC</strong></p> <pre><code>package forum9994762; import javax.xml.bind.annotation.*; @XmlAccessorType(XmlAccessType.FIELD) public class ClassC { @XmlValue private String var4; } </code></pre> <p><strong>Demo</strong></p> <pre><code>package forum9994762; import java.io.File; import javax.xml.bind.*; public class Demo { public static void main(String[] args) throws Exception { JAXBContext jc = JAXBContext.newInstance(ClassA.class); File xml = new File("src/forum9994762/input.xml"); Unmarshaller unmarshaller = jc.createUnmarshaller(); ClassA classA = (ClassA) unmarshaller.unmarshal(xml); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(classA, System.out); } } </code></pre> <p><strong>input.xml/Output</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt; &lt;Sample var2="value"&gt; &lt;data var4="var4 value"/&gt; &lt;/Sample&gt; </code></pre> <hr/> <p><strong>Mapping File</strong></p> <p>As a Castor user, you may prefer representing your metadata as an external mapping file. EclipseLink MOXy offers such an extension:</p> <p><strong>binding.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" package-name="forum9994762" xml-accessor-type="FIELD"&gt; &lt;java-types&gt; &lt;java-type name="ClassA"&gt; &lt;xml-root-element name="Sample"/&gt; &lt;java-attributes&gt; &lt;xml-element java-attribute="var1" name="data"/&gt; &lt;xml-attribute java-attribute="var2"/&gt; &lt;/java-attributes&gt; &lt;/java-type&gt; &lt;java-type name="ClassB"&gt; &lt;java-attributes&gt; &lt;xml-attribute java-attribute="var3" name="var4"/&gt; &lt;/java-attributes&gt; &lt;/java-type&gt; &lt;java-type name="ClassC"&gt; &lt;java-attributes&gt; &lt;xml-value java-attribute="var4"/&gt; &lt;/java-attributes&gt; &lt;/java-type&gt; &lt;/java-types&gt; &lt;/xml-bindings&gt; </code></pre> <p><strong>Demo</strong></p> <pre><code>package forum9994762; import java.io.File; import java.util.*; import javax.xml.bind.*; import org.eclipse.persistence.jaxb.JAXBContextFactory; public class Demo { public static void main(String[] args) throws Exception { Map&lt;String, Object&gt; properties = new HashMap&lt;String, Object&gt;(1); properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, "forum9994762/binding.xml"); JAXBContext jc = JAXBContext.newInstance("forum9994762", ClassA.class.getClassLoader(), properties); File xml = new File("src/forum9994762/input.xml"); Unmarshaller unmarshaller = jc.createUnmarshaller(); ClassA classA = (ClassA) unmarshaller.unmarshal(xml); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(classA, System.out); } } </code></pre> <p><strong>For More Infomation</strong></p> <ul> <li><a href="http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html" rel="nofollow">http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html</a></li> <li><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></li> </ul>
 

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