简体   繁体   English

Jackson 在同一个 POJO 中映射不同的展开元素

[英]Jackson map different unwrapped elements in same POJO

I am trying to parse some XML to Java, but this XML is a big mess.我正在尝试将一些 XML 解析为 Java,但是这个 XML 一团糟。

So looks like Jackson only read the last object of each type and the array size of each type has only one object.所以看起来 Jackson 只读取了每种类型的最后一个对象,并且每种类型的数组大小只有一个对象。

Edit.: This is what i am trying to do (debug and see the values of form object):编辑。:这就是我想要做的(调试并查看表单对象的值):

public class Main {

    public static void main(String[] args) throws IOException {
        XmlMapper mapper = new XmlMapper();
        Form form  = mapper.readValue(getXml(), Form.class);
    }

    public static String getXml() {

        return "<forms>\n"
            + "    <circles><x>1</x></circles>\n"
            + "    <squares><x>1</x></squares>\n"
            + "    <squares><x>1</x></squares>\n"
            + "    <circles><x>1</x></circles>\n"
            + "    <squares><x>1</x></squares>\n"
            + "</forms>";

    }

}

@JacksonXmlRootElement(localName = "forms")
class Form {

    @JacksonXmlElementWrapper(useWrapping = false)
    @JacksonXmlProperty(localName = "circles", isAttribute = false)
    private List<Circle> circles;

    @JacksonXmlElementWrapper(useWrapping = false)
    @JacksonXmlProperty(localName = "squares", isAttribute = false)
    private List<Square> squares;

    public List<Circle> getCircles() {
        return circles;
    }

    public void setCircles(List<Circle> circles) {
        this.circles = circles;
    }

    public List<Square> getSquares() {
        return squares;
    }

    public void setSquares(List<Square> squares) {
        this.squares = squares;
    }

}

@JacksonXmlRootElement(localName = "circles")
class Circle {

    @JacksonXmlProperty(isAttribute = false)
    private String x;

    public String getX() {
        return x;
    }

    public void setX(String x) {
        this.x = x;
    }

}

@JacksonXmlRootElement(localName = "squares")
class Square {

    @JacksonXmlProperty(isAttribute = false)
    private String x;

    public String getX() {
        return x;
    }

    public void setX(String x) {
        this.x = x;
    }

}

Initialize the list.初始化列表。

    @JacksonXmlElementWrapper(useWrapping = false)
    @JacksonXmlProperty(localName = "circles", isAttribute = false)
    private final List<Circle> circles = new ArrayList<>();

Rather than replace, add all elements.添加所有元素而不是替换。

    public void setCircles(List<Circle> circles) {
        this.circles.addAll(circles);
    }

This is the trick.这就是诀窍。 Thanks to @StaxMan.感谢@StaxMan。

Since lists are non-consecutive, what happens is that they will get separately created, so that last run overrides earlier.由于列表是不连续的,因此它们将被单独创建,以便上次运行更早地覆盖。 You may be able to resolve this by implementing setCircles (etc) to append to List, not replace it.您可以通过实现setCircles (etc) 来附加到 List 而不是替换它来解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM