Reputation: 3030
I'm using Spring Boot and I want to convert a POJO to XML. What is the Simplest way of doing it?
For example, I have a Person
POJO:
public class Person {
private String firstName;
private String lastName;
//getters/setters
}
How do I convert a List<Person>
to this:
<rootElement>
<person>
<firstName>John</firstName>
<lastName>Smith</lastName>
</person>
</rootElement>
And what class should I use to encapsulate it in? The equivalent for Jackson is JsonNode
from com.fasterxml.jackson.databind
package. Are there any preconfigured beans I can use from Spring Boot?
Upvotes: 2
Views: 7823
Reputation: 91
For converting a list into xml directly, I use javax.xml.bind.marshaller
.
You can annotate your pojo class as below
@XmlRootElement("Person")
@XmlAccessorType(XmlAccessType.FIELD)
public class Person {
private String firstName;
private String lastName;
//getters/setters
}
And make a List class wrapping it.
@XmlRootElement(name = "Persons_List")
public class Persons_List {
List<Person> persons;
// Getters and Setters
}
And you can use Jaxb in your method as below.
List<Person> persons = new List<Person>();
// add Person elements to it.
persons.add(person1);
persons.add(person2);
Persons_List persons_list = new Persons_List();
persons_list.setPersons(persons);
JAXBContext context = JAXBContext.newInstance(Persons_List.class, Person.class);
Marshaller jaxbMarshaller = context.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
//if you want to output to file.
OutputStream os = new FileOutputStream( "Person.xml" );
jaxbMarshaller.marshal(persons_list, os);
//if you want to display in console.
jaxbMarshaller.marshal(persons_list,new PrintWriter(System.out));
Output will be:
<Persons_List>
<Person>
<firstName>John</firstName>
<lastName>Smith</lastName>
</Person>
<Person>
<firstName>Will</firstName>
<lastName>Smith</lastName>
</Person>
</Persons_List>
Upvotes: 1
Reputation: 1163
Manually
You can use the mentioned Jackson library with XML dataformat:
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.9.8'
Serializing:
Person person = new Person("Ima", "Person")
XmlMapper xmlMapper = new XmlMapper();
String personXml = xmlMapper.writeValueAsString(person);
Deserializing:
XmlMapper xmlMapper = new XmlMapper();
Person person = xmlMapper.readValue(personXml, SimpleBean.class);
Via the REST API
I leave this section here as it might be relevant to others who use SpringBoot for a web server:
Or if you are using the standard spring-boot-starter-web and want to serve the output XML through a REST API, then Spring will automatically do the conversion for you. eg.the Person return type of this method means that Spring will automatically handle the conversion and transport for the output of personService.findById(id)
@GetMapping("/person")
public Person getPerson(@RequestParam("id") String id) {
return personService.findById(id);
}
By default it will serve your payload objects in JSON format but you can change it to XML by adding the above dependency for the Jackson XML data format
And additionally setting the Accept type in the request header as Application/XML
Upvotes: 2