'Using JAXB XmlElementWrapper my list is always null

I have this xml File:

<?xml version="1.0" encoding="UTF-8"?>
<myXML modelVersion="1.1.0">
  <A>
    <B>value</B>
    <C>
      <D>
        <E>value</E>
        <E>value</E>
        <E>value</E>
      </D>
      <D>
        <E>value</E>
        <E>value</E>
        <E>value</E>
      </D>
      <D>
        <E>value</E>
        <E>value</E>
        <E>value</E>
      </D>
    </C>
  </A>
</myXML>

So I have many D-Elements. I want to have a List<D> with D-Elements. I have this Classes:

@XmlRootElement(name = "myXml")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class MyXml {

    private A a;
    
    @XmlElement(name = "A")
    public A getA() {
        return a;
    }
}

Class A:

@XmlRootElement(name = "A")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class A {

    private String b;
    
    
    @XmlElementWrapper(name = "C")
    @XmlElement(name = "D")
    List<D> Ds;
    

    public List<D> getDs() {
        return Ds;
    }
    
    
    
    @XmlElement(name="B")
    public String getB() {
        return b;
    }   
}

Class D:

@XmlRootElement(name = "D")
@XmlAccessorType(XmlAccessType.FIELD)
public class D {

    
    private String e1;
    private String e2;
    private String e3;
    
    public String getE1() {
        return e1;
    }
    
    public String getE2() {
        return e2;
    }
    
    public String getE3() {
        return e3;
    }   
    
}

So I use the XmlElementWrapper in my Class A to wrap C. C contains many D-Elements. Whenever I initialize an Object of A, I can access the String b pretty well. But my List<D> Ds is always null, whatever I do.



Sources

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

Source: Stack Overflow

Solution Source