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="noreferrer"><strong>EclipseLink JAXB (MOXy)</strong></a> lead and a member of the <a href="http://jcp.org/en/jsr/detail?id=222" rel="noreferrer"><strong>JAXB (JSR-222)</strong></a> expert group,</p> <p>Below is how this can be done if you are using MOXy as your JAXB provider.</p> <ul> <li><a href="http://blog.bdoughan.com/2011/08/json-binding-with-eclipselink-moxy.html" rel="noreferrer">http://blog.bdoughan.com/2011/08/json-binding-with-eclipselink-moxy.html</a></li> </ul> <p><strong>JAVA MODEL</strong></p> <p><strong>Customer</strong></p> <pre><code>import java.util.*; import javax.xml.bind.annotation.*; @XmlRootElement(namespace="http://www.example.com") @XmlType(namespace="http://www.example.com") @XmlAccessorType(XmlAccessType.FIELD) public class Customer { @XmlAttribute private int id; @XmlElement(namespace="http://www.example.com") private String firstName; @XmlElement(namespace="http://www.example.com", nillable=true) private String lastName; @XmlElement(namespace="http://www.example.com") private List&lt;PhoneNumber&gt; phoneNumbers = new ArrayList&lt;PhoneNumber&gt;(); } </code></pre> <p><strong>PhoneNumber</strong></p> <pre><code>import javax.xml.bind.annotation.*; @XmlAccessorType(XmlAccessType.FIELD) public class PhoneNumber { @XmlAttribute private String type; @XmlValue private String number; } </code></pre> <p><strong>jaxb.properties</strong></p> <p>To specify 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="noreferrer">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>DEMO CODE</strong></p> <p><strong>input.xml</strong></p> <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;ns0:customer xmlns:ns0="http://www.example.com" id="123"&gt; &lt;ns0:firstName&gt;Jane&lt;/ns0:firstName&gt; &lt;ns0:lastName xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/&gt; &lt;ns0:phoneNumbers type="work"&gt;555-1111&lt;/ns0:phoneNumbers&gt; &lt;/ns0:customer&gt; </code></pre> <p><strong>Demo</strong></p> <p>In the demo code below we will use the same JAXB metadata to convert an XML document to Java objects, and then convert those objects back to JSON. With MOXy you can specify JSON output by setting a property on the <code>Marshaller</code>.</p> <pre><code>import java.io.File; import javax.xml.bind.*; import org.eclipse.persistence.jaxb.MarshallerProperties; public class Demo { public static void main(String[] args) throws Exception { JAXBContext jc = JAXBContext.newInstance(Customer.class); Unmarshaller unmarshaller = jc.createUnmarshaller(); File xml = new File("src/forum15357366/input.xml"); Customer customer = (Customer) unmarshaller.unmarshal(xml) ; Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json"); marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false); marshaller.marshal(customer, System.out); } } </code></pre> <p><strong>JSON Output</strong></p> <p>Below is the JSON output. Note how there are no indicators corresponding to namespaces or XML attributes. Also note the collection of size one was correctly represented as a JSON array (a problem with some other approaches).</p> <pre><code>{ "id" : 123, "firstName" : "Jane", "lastName" : null, "phoneNumbers" : [ { "type" : "work", "value" : "555-1111" } ] } </code></pre>
    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