Jackson: deserializing XML gives too many elements in list

DagR :

Trying to deserialize an XML like:

<?xml version="1.0" encoding="UTF-8"?>
<Items>
    <Item>
        <Element>
            <Link uri="urn:1">TestLC</Link>
        </Element>
        <Element2>
            <Link>link</Link>
        </Element2>
    </Item>
</Items>

Using code:

@JacksonXmlRootElement(localName = "Items")
@Data
@NoArgsConstructor
public class ItemInfo {

    @JacksonXmlProperty(localName = "Item")
    @JacksonXmlElementWrapper(useWrapping = false)
    private List<Item> items;

    @Data
    @NoArgsConstructor
    public static class Item {
        @JacksonXmlProperty(localName = "Element")
        private Element element;
    }

    @Data
    @NoArgsConstructor
    public static class Element {
        @JacksonXmlProperty(localName = "Link")
        private String link;
    }

    public static void main(String[] args) throws IOException {
        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
                + "<Items>"
                + "    <Item>"
                + "        <Element>"
                + "            <Link uri=\"urn:1\">TestLC</Link>"
                + "        </Element>"
                + "        <Element2>"
                + "            <Link>link</Link>"
                + "        </Element2>"
                + "    </Item>"
                + "</Items>";

        XmlMapper xmlMapper = new XmlMapper();
        xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        ItemInfo itemInfo = xmlMapper.readValue(xml, ItemInfo.class);
        System.out.println(itemInfo.getItems().size());
    }
}

I was expecting the output 1, that is an itemInfo with items containing one element corresponding to the single <Item> tag.

However the output is 2. Somehow the parser thinks there are two <Item>s

I don't understand what is going on here and what is wrong. If I, for example, remove the attribute uri, the result is as expected.

Contents of itemInfo.getItems():

[ItemInfo.Item(element=ItemInfo.Element(link=TestLC)), ItemInfo.Item(element=null)]

I'm using jackson-dataformat-xml version 2.8.10

Michał Ziober :

You do not need wrapper ItemInfo because we can treat it like a wrapper for Item elements. You can simplify your code to:

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import java.io.File;
import java.util.Arrays;

public class XmlApp {

  public static void main(String[] args) throws Exception {
    File xmlFile = new File("./resources/test.xml");

    XmlMapper xmlMapper = new XmlMapper();
    xmlMapper.setDefaultUseWrapper(true);
    xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    Item[] itemInfo = xmlMapper.readValue(xmlFile, Item[].class);
    System.out.println(Arrays.toString(itemInfo));
  }
}

class Item {
  @JacksonXmlProperty(localName = "Element")
  private Element element;

  // getters, setters, toString
}

class Element {
  @JacksonXmlProperty(localName = "Link")
  private String link;

  // getters, setters, toString
}

Above code prints:

[Item{element=Element{link='TestLC'}}]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=141511&siteId=1