我无法理解为什么解编。
这是我的班级:
@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>
我需要你的帮助
答案 0 :(得分:1)
您的映射不正确。
尝试:
@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
}