Convert json to xml

There is various libraries that convert json to XML and in this example we will show how to convert json to XML using jackson's ObjectMapper and XmlMapper. In the set up, a browser class was created to hold browser information which we will map to below.


class Browser {
    private String name;

    public Browser(String name) {
        super(); = name;
    public String toString() {
        return Objects.toStringHelper(this)
                .add("name", name)

    public String getName() {
        return name;


First we will create a string of json that represents a list of browsers. Next we will call ObjectMapper.readValue to transform the json into a list of browsers. Once converted we will then write the list of objects as xml.

public void convert_json_to_xml_jackson () throws IOException {
    String browsersAsJson ="[{\"name\":\"Chrome\"},{\"name\":\"FireFox\"},{\"name\":\"Internet Explorer\"}]\n";
    ObjectMapper jsonMapper = new ObjectMapper();
    List<Browser> browsers = jsonMapper.readValue(browsersAsJson, List.class);

    XmlMapper xmlMapper = new XmlMapper();
    String browsersAsXml = xmlMapper.writeValueAsString(browsers);;

    assertTrue(browsersAsXml.length() > 0);


<?xml version="1.0" encoding="UTF-8"?>
      <name>Internet Explorer</name>