JAXB解组不同类型的列表

时间:2016-02-02 11:31:12

标签: java xml jaxb

我有以下xml:

<scope>
   <heading>
      <code></code>
   </heading>
   <heading>
      <code></code>
   </heading>
   <value>
      <code></code>
   </value>
   <value>
      <code></code>
   </value>
</scope>

我如何进行解组,以便我有两个列表。 感谢。

1 个答案:

答案 0 :(得分:2)

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public static class Scope {
    @XmlElement(name = "heading")
    List<Heading> headings = new LinkedList<>();
    @XmlElement(name = "value")
    List<Value> values = new LinkedList<>();

    public Scope() {
    }

    public List<Heading> getHeadings() {
        return headings;
    }

    public void setHeadings(List<Heading> headings) {
        this.headings = headings;
    }

    public List<Value> getValues() {
        return values;
    }

    public void setValues(List<Value> values) {
        this.values = values;
    }
}

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public static class Heading {
    private String code;

    public Heading() {
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }
}

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public static class Value {
    private String code;

    public Value() {
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }
}