JAXB Marshalling a variable list of elements with the same name

26,810

You need to do the following:

  • annotate the element property on the Element class with @XmlValue.
  • make sure the case of the element names in the annotations matches the names in the XML document.

For More Information

Share:
26,810

Related videos on Youtube

user1816198
Author by

user1816198

Updated on January 16, 2020

Comments

  • user1816198
    user1816198 over 4 years

    As per the title, I have an XML file I need to unmarshal:

    <?xml version="1.0"?>
    <root>
        <wrap>
            <Element>something1</Element>
            <Element>something2</Element>
            <Element>something3</Element>
        </wrap>
    </root>
    

    "wrap" is simply a wrapper, but the count of "element" varies.

    I have two classes to facilitate objects for JAXB:

    wrap class:

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlRootElement(name = "root")
    public class Wrap {
        @XmlElementWrapper(name = "wrap")
        @XmlElement(name = "Element")
        private List<Element> elementList = new ArrayList<>();
    
        public Wrap() {}
    
        public Wrap(List<Element> list) {
            this.elementList = list;
        }
    
        public void addElement(Element element) {
            this.elementList.add(element);
        }
    
        public List<Element> getWrap() {
            return this.elementList;
        }
    
        public void setWrap(List<Element> wrap) {
            this.elementList = wrap;
        }
    }
    

    element class:

    @XmlRootElement(name = "Element")
    public class Element {
    
        private String Element;
    
        public Element() {}
    
        public Element(String element) {
            this.Element = element;
        }
    
        public String getElement() {
            return Element;
        }
    
        public void setElement(String element) {
            this.Element = element;
        }
    }
    

    Attempting to unmarshal the XML completes without error, however, the element values are not stored with the element objects. Instead toString returns null for each of the objects.

    I did populate the objects with some data and marshal them to a file (shown below). This format, of course, is incorrect and should match the XML above.

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <root>
        <wrap>
            <Element>
                <element>entry1</element>
            </Element>
            <Element>
                <element>entry2</element>
            </Element>
            <Element>
                <element>entry3</element>
            </Element>
        </wrap>
    </root>
    

    I've researched this for awhile now with the assumptions my annotations are incorrect, but perhaps it's something else...

    • Helios
      Helios over 10 years
      Remove the annotation @XmlRootElement(name = "Element") from your Element class. I guess @XmlElement(name = "Element") is supposed to be @XmlElement(name = "element") in wrap class. isn't it?
  • user1816198
    user1816198 over 10 years
    This worked! Although in addition to the @XmlValue tag I added "@XmlAccessorType(XmlAccessType.FIELD)" above "@XmlRootElement(name = "Element")". Thanks so much!