gpt4 book ai didi

java - JAXB 解码返回属性的空值

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:07:17 25 4
gpt4 key购买 nike

你能诊断一下为什么我的代码打印出 [null, null, null, null] 吗:

Unmarshaller m = JAXBContext.newInstance(Roles.class).createUnmarshaller();
Roles root = m.ummarshal(new FileReader("test.xml"));
System.out.println(root);

我尝试在 eclipse 中调试,但 setter 和 getter 处的断点没有命中

test.xml

<?xml version="1.0" encoding="UTF-8" ?>
<Roles>
<Ele Id="1" Value="Yes"/>
<Ele Id="2" Value="Yes"/>
<Ele Id="3" Value="No"/>
<Ele Id="4" Value="Yes"/>
</Roles>

角色.java

@XmlRootElement(name="Roles")
public class Roles {
private List<Ele> EleList;
public Roles() {super();}

@XmlElement(name="Ele")
public List<Ele> getEleList() {return EleList;}
public void setEleList(List<Ele> EleList) {this.EleList = EleList;}
public String toString() {return EleList.toString();}
}

Ele.java

public class Ele {

@XmlAttribute
private String Id;

@XmlAttribute
private String Value;

public Ele(){super();}

public String getId() {return Id;}
public void setId(String id) {Id = id;}
public String getValue() {return Value;}
public void setValue(String value) {Value = value;}

public String toString() { if(Id == null || Value == null) return null; else return Id + Value;}
}

最佳答案

我自己解决了这个问题。

你需要写:@XmlAttribute(name="Id") & @XmlAttribute(name="Value") getId()getValue() 代替了 @XmlAttribute。标识符名称未被拾取。

关于java - JAXB 解码返回属性的空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7991600/

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