Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamic JAXB support to convert XML to JSON
    text
    copied!<p>I am using eclipse link(v2.5.0) Dynamic JAXB to convert XML to JSON and viceversa.</p> <p>customer.xsd</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt; &lt;xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt; &lt;xs:element name="address" type="address"/&gt; &lt;xs:element name="customer" type="customer"/&gt; &lt;xs:complexType name="address"&gt; &lt;xs:sequence&gt; &lt;xs:element name="city" type="xs:string" minOccurs="0"/&gt; &lt;xs:element name="street" type="xs:string" minOccurs="0"/&gt; &lt;xs:element name="type" type="xs:string"/&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;xs:complexType name="customer"&gt; &lt;xs:sequence&gt; &lt;xs:element ref="address" minOccurs="0"/&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;/xs:schema&gt; </code></pre> <p>customer.xml</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;customer&gt; &lt;name&gt;Jane Doe&lt;/name&gt; &lt;address&gt; &lt;city&gt;My Town&lt;/city&gt; &lt;street&gt;123 Any Street&lt;/street&gt; &lt;type&gt;work&lt;/type&gt; &lt;/address&gt; &lt;/customer&gt; </code></pre> <p>customer.json</p> <pre><code>{ "address" : { "city" : "My Town", "street" : "123 Any Street", "type" : "work" } } </code></pre> <p>MyCode</p> <pre><code>public class Demo { public static void main(String[] args) { try { // create DynamicJAXBContext FileInputStream xsdInputStream = new FileInputStream("D:\\GUI\\customer.xsd"); DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromXSD(xsdInputStream, null, null, null); // Unmarshal XML--&gt; Java FileInputStream xmlInputStream = new FileInputStream("D:\\GUI\\customer.xml"); JAXBUnmarshaller unmarshaller = jaxbContext.createUnmarshaller(); JAXBElement&lt;DynamicEntity&gt; root = (JAXBElement)unmarshaller.unmarshal(xmlInputStream); JAXBMarshaller marshaller = jaxbContext.createMarshaller(); DynamicEntity javaResponse = root.getValue(); Map namespaces = new HashMap(); // Marshal Java --&gt; JSON JAXBMarshaller jsonMarshaller = jaxbContext.createMarshaller(); jsonMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); jsonMarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json"); jsonMarshaller.setProperty(MarshallerProperties.NAMESPACE_PREFIX_MAPPER, namespaces); FileOutputStream jsonOutputStream = new FileOutputStream("D:\\GUI\\customer.json"); jsonMarshaller.marshal(javaResponse, jsonOutputStream); // JSON-&gt;JAVA-&gt;XML JAXBUnmarshaller jsonUnmarshaller = jaxbContext.createUnmarshaller(); jsonUnmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json"); jsonUnmarshaller.setProperty(UnmarshallerProperties.JSON_NAMESPACE_PREFIX_MAPPER, namespaces); StreamSource json = new StreamSource("D:\\GUI\\customer.json"); JAXBElement&lt;DynamicEntity&gt; myroot = (JAXBElement)jsonUnmarshaller.unmarshal(json); DynamicEntity myResponse = myroot.getValue(); marshaller.marshal(myResponse, System.out); } catch (JAXBException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } } } </code></pre> <p>Exception</p> <pre><code>Exception in thread "main" java.lang.NullPointerException at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:264) at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:443) at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:296) at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parseRoot(JSONReader.java:166) at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:125) at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:140) at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:778) at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:666) at org.eclipse.persistence.oxm.XMLUnmarshaller.unmarshal(XMLUnmarshaller.java:593) at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:287) at Demo.main(Demo.java:47) </code></pre> <p>My questions</p> <p>1.Does eclipse link Dynamic JAXB officially support XML to JSON and viceversa conversions as I have tried above, as I could not see any such examples?</p> <p>2.How to avoid the above nullpointer exception and still have an element named "type" defined as part of the schema? Is this a bug? Are there any workarounds? I have written the demo code only to highlight the same problem I face elsewhere where I use multiple XML schemas and require a namespace aware handling for JSON conversion.</p>
 

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