JAXB编组具有相同名称的元素的变量列表

时间:2022-09-25 10:36:37

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

根据标题,我有一个我需要解组的XML文件:

<?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.

“wrap”只是一个包装器,但“元素”的数量会有所不同。

I have two classes to facilitate objects for JAXB:

我有两个类来为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.

然而,尝试解组XML完成而没有错误,元素值不与元素对象一起存储。相反,toString为每个对象返回null。

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相匹配。

<?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...

我已经研究了一段时间了,假设我的注释是不正确的,但也许是其他的......

1 个解决方案

#1


4  

You need to do the following:

您需要执行以下操作:

  • annotate the element property on the Element class with @XmlValue.
  • 使用@XmlValue注释Element类的element属性。
  • make sure the case of the element names in the annotations matches the names in the XML document.
  • 确保注释中元素名称的大小写与XML文档中的名称相匹配。

For More Information

了解更多信息

#1


4  

You need to do the following:

您需要执行以下操作:

  • annotate the element property on the Element class with @XmlValue.
  • 使用@XmlValue注释Element类的element属性。
  • make sure the case of the element names in the annotations matches the names in the XML document.
  • 确保注释中元素名称的大小写与XML文档中的名称相匹配。

For More Information

了解更多信息