Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB unmarshal returning null values for attributes

Tags:

java

xml

jaxb

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;}
}
like image 906
satish b Avatar asked Nov 03 '11 07:11

satish b


1 Answers

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.

like image 74
satish b Avatar answered Oct 10 '22 01:10

satish b