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 <strong><a href="http://jcp.org/en/jsr/detail?id=222" rel="nofollow">JAXB (JSR-222)</strong></a> expert group.</p> <p>Since you already have established JAXB mappings and are converting XML to JSON, you may be interested in EclipseLink JAXB (MOXy) which offers both object-to-XML and object-to-JSON mapping using the same JAXB metadata.</p> <p><strong>Customer</strong></p> <p>Below is a sample model with JAXB annotations.</p> <pre><code>package forum11599191; import java.util.List; import javax.xml.bind.annotation.*; @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class Customer { @XmlAttribute private int id; private String firstName; @XmlElement(nillable=true) private String lastName; private List&lt;String&gt; email; } </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> in the same package as your domain model 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> <p><strong>input.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;customer id="123" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt; &lt;firstName&gt;Jane&lt;/firstName&gt; &lt;lastName xsi:nil="true"/&gt; &lt;email&gt;jdoe@example.com&lt;/email&gt; &lt;/customer&gt; </code></pre> <p><strong>Demo</strong></p> <p>The following demo code will populate the objects from XML and then output JSON. Note how there are no compile time dependencies on MOXy.</p> <pre><code>package forum11599191; import java.io.File; import javax.xml.bind.*; public class Demo { public static void main(String[] args) throws Exception { JAXBContext jc = JAXBContext.newInstance(Customer.class); // Unmarshal from XML Unmarshaller unmarshaller = jc.createUnmarshaller(); File xml = new File("src/forum11599191/input.xml"); Customer customer = (Customer) unmarshaller.unmarshal(xml); // Marshal to JSON Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty("eclipselink.media-type", "application/json"); marshaller.marshal(customer, System.out); } } </code></pre> <p><strong>JSON Ouput</strong></p> <p>Below is the output from running the demo code. </p> <pre><code>{ "customer" : { "id" : 123, "firstName" : "Jane", "lastName" : null, "email" : [ "jdoe@example.com" ] } } </code></pre> <p>A few things to note about the output:</p> <ol> <li>Since the <code>id</code> field is a numeric type it was marshalled to JSON without quotes.</li> <li>Even though the <code>id</code> field was mapped with <code>@XmlAttribute</code> there are no special indication of this in the JSON message.</li> <li>The <code>email</code> property had a <code>List</code> of size one, this is properly represented in the JSON output.</li> <li>The <code>xsi:nil</code> mechanism was used to specify that the <code>lastName</code> field had a <code>null</code> value, this has been translated to the proper null representation in the JSON output.</li> </ol> <p><strong>For More Information</strong></p> <ul> <li><a href="http://blog.bdoughan.com/2011/08/json-binding-with-eclipselink-moxy.html" rel="nofollow">http://blog.bdoughan.com/2011/08/json-binding-with-eclipselink-moxy.html</a></li> <li><a href="http://blog.bdoughan.com/2012/04/binding-to-json-xml-handling-null.html" rel="nofollow">http://blog.bdoughan.com/2012/04/binding-to-json-xml-handling-null.html</a></li> <li><a href="http://blog.bdoughan.com/2011/04/jaxb-and-json-via-jettison.html" rel="nofollow">http://blog.bdoughan.com/2011/04/jaxb-and-json-via-jettison.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. VO
      singulars
      1. This table or related slice is empty.
    2. 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