'Jackson XML: How to have different element names from list name?

The name of the list is present in the elements that belong to it.

public class root {
  @JacksonXmlProperty(isAttribute = true, localName = “Name”)
  private String name;

  @JsonProperty(“ItemList”)
  private list<Item> itemList = new ArrayList<>();
}

public class item {
  @JacksonXmlProperty(isAttribute = true, localName = “ItemName”)
  private String itemName;

  @JacksonXmlText
  private String value;
}

I get:

<root Name=“”>
  <ItemList>
    <ItemList ItemName=“”></ItemList>
  </ItemList>
</root>

I want:

<root Name=“”>
  <ItemList>
    <Item ItemName=“”></Item>
  </ItemList>
</root>

Thank you for any help



Solution 1:[1]

You must use annotations: @JacksonXmlElementWrapper(localName = "ItemList") and @JacksonXmlProperty(localName = "Item")

Example:

public class root {
    @JacksonXmlProperty(isAttribute = true, localName = "Name")
    public String name;

    @JacksonXmlElementWrapper(localName = "ItemList")
    @JacksonXmlProperty(localName = "Item")
    public List<Item> itemList = new ArrayList<>();
}

XML generated:

<root Name="123">
    <ItemList>
        <Item ItemName="ItemName">value</Item>
    </ItemList>
</root>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Eugene