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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21348006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:11:54  来源:igfitidea点击:

JAXB Marshalling a variable list of elements with the same name

javaxmljaxbannotations

提问by user1816198

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

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

采纳答案by bdoughan

You need to do the following:

您需要执行以下操作:

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

For More Information

想要查询更多的信息