I am trying to create the XML using the marshaling
method from the JAXB
library. For this approach, I am using the standard java class and assigning the annotation to it. I would like to have a wrapper element to some of the element in the class which are not Collections
.
I am ware of @XmlElementWrapper
which can be used for Collections
but I do not have a collection. I have some elements for which I want to have an outer XML element so that it can match the standard XSD.
I just want to know if there is a way to add an outer XML element to some of the elements so that the create XML matches the standard XSD format. If there is no direct approach then what alternative approach can I take?
As we can see from the example XML the carinfo
tag is a outer (wrapper) tag for the elements engine
and year
but this carInfo
tag is not present within the Parent
or Child
class. As these are standard classes I cannot modify the fields/ Add new fields. Hence I would like to handle wrapper XML tag addition using the JAXB.
Following is the input JSON:
{
"brand": "Ferari",
"build": "Italy",
"engine": "Mercedes",
"year": "2021"
}
Following is the output XML that I would like to have:
<?xml version="1.0"?>
<Car>
<brand>Ferari</brand>
<build>Italy</build>
<carinfo>
<engine>Mercedes</engine>
<year>2021</year>
</carinfo>
</Car>
Following are my classes:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlTransient
@XmlType(name = "Parent")
public class Parent{
private String brand;
private String year;
//Getters and Setters avoided
}
Class based on which XML is being created:
@XmlRootElement(name = "Car")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Child",propOrder={"brand","engine","build","year"})
public class Child extends Parent{
private String engine;
private String build;
//Getter and Setters avoided
}
The main class which will create the XML: I am using the Jackson
library to read the values from the Json
file and create the XML based on Jaxb
annotations.
public class Main{
public static void main(String []args){
JsonFactory jsonFactory = new JsonFactory();
JsonParser jsonParser = jsonFactory.createParser(new File(Main.class.getClassLoader().getResource("input.json").toURI()));
Child eventInfo = jsonParser.readValueAs(Child.class);
JAXBContext context = JAXBContext.newInstance(Child.class);
Marshaller mar = context.createMarshaller();
mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
mar.marshal(eventInfo, System.out);
}
}