I have a JSON file that I am trying to convert into XML
using the JAXB
annotation approach. Everything is working fine now and I able to convert the JSON
to XML
. Now I am trying to refactor the code a little bit so that my class would look clean. Hence, I am trying to remove the method which is present in my class
and make it JAXB XMLAdapter
so that it can be reused by other classes.
Basically I would like to move the XMLSupport
method from CarInfo
class to XMLAdapter
. I am not sure how to populate the CarInfo
objects when I move them to the XMLAdapter
.
Following is my JSON
file (it has been modified for simplicity purpose):
{
"brand": "Ferari",
"build": "Italy",
"engine": "Mercedes",
"year": "2021"
}
Following is the XML
that I expect JAXB
to provide: (Observe the carInfo
tag which is not present in JSON
but I need in XML
to match the standard XSD
)
<?xml version="1.0"?>
<Car>
<brand>Ferari</brand>
<build>Italy</build>
<carinfo>
<engine>Mercedes</engine>
<year>2021</year>
</carinfo>
</Car>
Following are the classes that I have: (Tha Car
class that matches the JSON elements)
@XmlAccessorType(XmlAccessType.FIELD)
@XmlTransient
@XmlSeeAlso({MyCar.class});
public class Car{
private String brand;
private String build;
@XmlTransient
private String engine;
@XmlTransient
private String year;
//Getter, Setters and other consturctiores ommited
}
Following is MYCar
class that builds the XML
by adding the carInfo
tag:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Car")
@XmlType(name = "Car", propOrder = {"brand","build", "carInfo"})
public class MyCar extends Car{
@XmlElement(name="carInfo")
private CarInfo carInfo;
public MyCar xmlSupport() {
if(carInfo == null){
carInfo = new Carinfo();
}
carInfo.setEngine(getEngine);
carInfo.setYear(getYear());
return this;
}
}
Following is my CarInfo
class which acts as a helper to build the additional
tag around MyCar
class:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = {"engine","year"})
public class Carinfo{
private String engine;
private String year;
//Getter, Setters and other consturctiores ommited
}
Following is my Main
class which actually builds the XML
by using the JAXBCOntext
public class Main{
public static void main(String[] args){
JAXBContext context = JAXBContext.newInstance(MyCar.class);
Marshaller mar = context.createMarshaller();
mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
mar.marshal((MyCar).xmlSupport(), System.out);
System.out.println("-----------------");
}
}
Now coming back to my main question:
As we can see from MyCar
class I have the XMLSupport
method which is actually populating the CarInfo
objects and then using that method I am creating the XML
. Is there a way I can move this to XMLAdapter
?
I tried creating the XMLAdapter
but I am not sure how can I populate the CarInfo
objects from the adapter:
public class MyCar extends Car{
@XmlElement(name="carInfo")
@XmlJavaTypeAdapter(ExtensionAdapter.class)
@XmlElement(name = "carInfo")
private CarInfo carInfo;
}
Following is my Adapter
class I've tried:
public class ExtensionAdapter extends XmlAdapter<CarInfo, CarInfo> {
@Override
public CarInfo unmarshal(CarInfo valueType) throws Exception {
System.out.println("UN-MARSHALLING");
return null;
}
@Override
public CarInfo marshal(CarInfo boundType) throws Exception {
System.out.println("MARSHALLING");
System.out.println(boundType);
//I get boundType as NULL so I am not sure how to convert the xmlSupport Method to Adapter so I can use this adapter with multiple class
return null;
}
}