gpt4 book ai didi

java - 解码时发生错误

转载 作者:行者123 更新时间:2023-11-30 06:18:46 25 4
gpt4 key购买 nike

我不明白为什么要解码。

这是我的类(class):

@XmlRootElement(name = "privileges")
@XmlAccessorType(XmlAccessType.FIELD)
public class Privilege {
@XmlAttribute(name = "number_residents")
private Integer numberResidents;
@XmlAttribute(name = "value")
private String value;
@XmlElement(name = "privilege")
private String privilage;

位于 getter、setter、equals 和 toString 之下}

我正在尝试进行解码。

 File file = new File( "response.xml");
JAXBContext context = JAXBContext.newInstance(Privilege.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
Privilege privilege = (Privilege) unmarshaller.unmarshal(file);
System.out.println(privilege);

但我总是得到:

Privilege{numberResidents=null, value='null', privilage='Test privilege'}

但是在这样的文件中:

<?xml version="1.0" encoding="UTF-8"?>
<privileges>
<privilege number_residents="3" value="bb2">Test privilege</privilege>
</privileges>

我需要你的帮助

最佳答案

您的映射不正确。

尝试:

@XmlRootElement(name = "privileges")
@XmlAccessorType(XmlAccessType.FIELD)
public class Privileges {

@XmlElement(name="privilege")
private Privilege privilege;

//Getters and setters

}

@XmlAccessorType(XmlAccessType.FIELD)
public class Privilege{

@XmlAttribute(name = "number_residents")
private Integer numberResidents;
@XmlAttribute(name = "value")
private String value;
@XmlValue
private String privilege;

//getters and setters

}

关于java - 解码时发生错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48619387/

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