gpt4 book ai didi

java - JAXB:如何获取包装类型的值(value)?

转载 作者:数据小太阳 更新时间:2023-10-29 02:16:44 24 4
gpt4 key购买 nike

@XmlRootElement(name = "toplist")
class toplist {

private String description;

private List<Item> items= new ArrayList<Item>();

@XmlElement(name = "description")
public String getDescription() {...}
public String setDescription() {...}

@XmlElement(name = "item")
@XmlElementWrapper(name = "items")
public List<ChartResultItem> getToplistEntries() {...}
}

class Item {
private String attr1;
private String attr2;
private String attr3;

// getter and setter with
// @XmlAttribute(name = "atter1"), @XmlAttribute(name = "atter2") and etc
}

和 xml

<?xml version="1.0" encoding="UTF-8"?>
<toplist>
<description>This is description.</description>
<items>
<item attr1="" attr2="" attr3="">value1</item>
<item attr1="" attr2="" attr3="">value2</item>
<item attr1="" attr2="" attr3="">value3</item>
...
</items>
</toplist>

如何在 Item 类中获取 value1、value2、value3 等(通过 jaxb)?

最佳答案

Jom 的回答是正确的 (+1)。下面举个例子进一步说明:

@XmlAccessorType(XmlAccessType.FIELD)
public class Item {
@XmlValue
protected String value;

@XmlAttribute
protected String attr1;

@XmlAttribute
protected String attr2;

@XmlAttribute
protected String attr3;
}

关于java - JAXB:如何获取包装类型的值(value)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5815944/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com