Can you please diagnose why my code prints out [null, null, null, null]:
Unmarshaller m = JAXBContext.newInstance(Roles.class).createUnmarshaller();
Roles root = m.ummarshal(new FileReader("test.xml"));
System.out.println(root);
I tried debugging in eclipse but breakpoints at the setters and getters don't hit
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>
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;}
}
I solved the problem myself.
You need to write: @XmlAttribute(name="Id")
& @XmlAttribute(name="Value")
above the getId()
and getValue()
in place of just @XmlAttribute
. The identifier names are not picked up.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With