0

I am learning to use Jackson to serialize XML. My class structure is as below.

class City {
    @JacksonXmlProperty(localName = "CityName")
    String cityName;

    public City(String cityName) {
        this.cityName = cityName;
    }

    public String getcityName() {
        return cityName;
    }

    public void setcity(String cityName) {
        this.cityName = cityName;
    }
}

@JacksonXmlRootElement(localName = "Person")
class Person {
    @JacksonXmlProperty(localName = "name")
    private String name;

    @JacksonXmlProperty(localName = "age")
    private String age;

    @JacksonXmlProperty(localName = "city")
    private List<City> city;

    public Person() { }

    Person(String name, String age, List<City> city) {
        this.name = name;
        this.age = age;
        this.city = city;
    }

    public String getname() {
        return name;
    }

    public String getage() {
        return age;
    }

    public List<City> getcity() {
        return city;
    }

    public void setname(String name) {
        this.name = name;
    }

    public void setage(String age) {
        this.age = age;
    }

    public void setcity(List<City> city) {
        this.city = city;
    }
}

When I try to serialize a class to XML using Jackson, I get two tags for <city>

public class App
{
    public static void main( String[] args )
    {
        try {
            XmlMapper xmlMapper2 = new XmlMapper();
            Person p = new Person();
            City c1 = new City("abc");
            City c2 = new City("def");
            City c3 = new City("ghi");
            List<City> cityList = new ArrayList<City>();
            cityList.add(c1);
            cityList.add(c2);
            cityList.add(c3);
            p.setname("setattr");
            p.setage("55");
            p.setcity(cityList);
            xmlMapper2.enable(SerializationFeature.INDENT_OUTPUT);
            String respPerson = xmlMapper2.writeValueAsString(p);
            System.out.println(respPerson);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

This is the output that I get.

<Person><name>setattr</name><age>55</age><city><city><CityName>sfo</CityName></city><city><CityName>sjc</CityName></city><city><CityName>sea</CityName></city></city></Person>

Can you help me to understand why do I get two tags for city and how I can fix it?

I would like the output to be something like,

<Person><name>setattr</name><age>55</age><city><CityName>sfo</CityName><CityName>sjc</CityName><CityName>sea</CityName></city></Person>
Anit
  • 345
  • 1
  • 5
  • 19
  • Can you please tell how this is a duplicate? I have already checked that post. Any suggestion on how I can fix my code to make it work? – Anit Apr 14 '17 at 01:14
  • Using @JacksonXmlElementWrapper(useWrapping = false), I still see that the tag get surrounded for each tag. Instead I want just one tag that surrounds all cityNames – Anit Apr 14 '17 at 01:18

1 Answers1

1

You have a list of cities. Jackson is using 'city' for both the list itself and the members of the list. If you change the local name to 'cities', you might like the results better.

Unfortunately, this isn't the right answer. It appears, rather, that the right answer is provided https://stackoverflow.com/a/27144625/131433.

Community
  • 1
  • 1
bmargulies
  • 97,814
  • 39
  • 186
  • 310