I trying to use the JAXB annotation to convert the JSON data to XML. I have the JSON somewhat like this:
{
"Employee": {
"name": "Tanmay",
"Email": "tanmaypatil@xyz.com",
"Address": {
"City": "Florida",
"State": "Miami"
}
}
}
I would like to use marshalling to create the XML which would look something like this:
(Please note the extension tag which is not present in JSON but I need to add in XML)
<Company>
<Employee>
<FirstName>Tanmay</FirstName>
<extension>
<Address>
<City>Florida</City>
<State>Miami</State>
</Address>
</extension>
</Employee>
</Company>
I have the following classes:
My main class would actually read the JSON data and marshal it to XML. I have used Jackson to read the JSON data for simplicity purpose I have removed that code:
public class Main
{
public static void main(String[] args) {
JsonFactory jsonFactory = new JsonFactory();
JsonParser jsonParser = jsonFactory.createParser(new File(Main.class.getClassLoader().getResource("InputEPCISEvents.json").toURI()));
//I am using the JAKSON jsonParser to read the JSON file and automatically map it to the child classes
//Ommiting the JACKSON JsonParser code for simplicity purpose
JAXBContext context = JAXBContext.newInstance(Employee.class);
Marshaller mar = context.createMarshaller();
mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
mar.marshal(eventInfo, System.out);
System.out.println("-----------------");
}
}
Following is my Parent class which would be mapped to Incoming JSON:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlTransient
public class Employee{
private String name;
private String email;
private Address address;
//getters and setters are ommited
//Noargs and allargs constuctor ommited
}
Following is my Address class:
@XmlRootElement(name = "Employee")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Extension", propOrder = { "name","email","city","state"})
private class Address extends Employee{
@XmlElement(name = "extension")
private Extension extension;
public Extension getExtension() {
extension.setCity(getCity());
extension.setState(getState());
return extension;
}
public Extension setExtension(Extension extension) {
this.setCity(extension.getCity());
this.setState(extension.getState());
this.extension = extension;
}
}
Following is the Extension class that I have created as Extension tag will not be present within the incoming JSON but I need to add it to the XML during the marshaling:
@XmlAccessorType(XmlAccessType.FIELD)
public class Extension{
private String city;
private String state;
//getters and setters are ommited
//Noargs and allargs constuctor ommited
}
I would like to know how to do the following things using the JAXB. I am fine with creating some additional classes or modifying the existing class but I cannot change the incoming JSON or XML that I am creating as they are standard format.
How can I map the
address,city,statefrom JSON to Class object. If the elements are present within theAddress.classthen it can be mapped automatically but since I want theextensiontag inXMLI have moved them to myExtension.classhence theJAXBcannot recognize it and its not mapping to objects.How to add the additional
extensiontag to theXMLthat will be created during the marshalling. As we can see theJSONdoes not contain theextensiontag but I need it in theXML. If its collection then I can use the@XmlElementWrapperbut in my case it's not Collection.
I tried searching a lot but could not find anything that helps me. So based on my understanding I have done above mentioned things. Any help or suggestions would be really appreciated.