Note. I am EclipseLink JAXB (MOXy) and a member of the JAXB 2 group (JSR-222) .
You can use MOXy to handle both aspects of the XML binding and JSON in this use case. As I mentioned in my comment, MOXy supports the XMLGregorianCalendar type. Metadata will look like this:
EmpRequest
package forum7725188; import javax.xml.bind.annotation.*; @XmlRootElement(name="EmpRequest") @XmlAccessorType(XmlAccessType.FIELD) public class EmpRequest { @XmlElement(name="EmplIn") private EmplIn emplIn; }
EmplIn
package forum7725188; import javax.xml.bind.annotation.*; import javax.xml.datatype.XMLGregorianCalendar; @XmlAccessorType(XmlAccessType.FIELD) public class EmplIn { @XmlElement(name="EmpID") private long empId; @XmlElement(name="Empname") private String name; @XmlElement(name="Designation") private String designation; @XmlElement(name="DOJ") private XMLGregorianCalendar doj; }
package info
@XmlSchema(namespace="http://java.com/Employee", elementFormDefault=XmlNsForm.QUALIFIED) @XmlAccessorType(XmlAccessType.FIELD) package forum7725188; import javax.xml.bind.annotation.*;
Demo
You can configure the MOXy implementation of Marshaller to output JSON by setting the eclipselink.media-type property to application/json .
package forum7725188; import java.io.File; import javax.xml.bind.*; import javax.xml.namespace.QName; public class Demo { public static void main(String[] args) throws Exception { JAXBContext jc = JAXBContext.newInstance(EmpRequest.class); Unmarshaller unmarshaller = jc.createUnmarshaller(); File xml = new File("src/forum7725188/input.xml"); EmpRequest empRequest = (EmpRequest) unmarshaller.unmarshal(xml); JAXBElement<EmpRequest> jaxbElement = new JAXBElement<EmpRequest>(new QName(""), EmpRequest.class, empRequest); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty("eclipselink.media-type", "application/json"); marshaller.marshal(jaxbElement, System.out); } }
Input.xml
<?xml version="1.0" encoding="UTF-8"?> <EmpRequest xmlns="http://java.com/Employee"> <EmplIn> <EmpID>12</EmpID> <Empname>sara</Empname> <Designation>SA</Designation> <DOJ>2002-05-30T09:30:10+06:00</DOJ> </EmplIn> </EmpRequest>
Output
{"EmplIn" : {"EmpID" : "12", "Empname" : "sara", "Designation" : "SA", "DOJ" : "2002-05-30T09:30:10+06:00"}}
Additional Information
source share