I have a simple stock XML file with the following format:
<?xml version="1.0" encoding="UTF-8"?>
<estoque>
<item Nome="Impressora XL2N" Peso="13 kg" Armazem="8" Quantidade="12" Preco="R$ 8505,00" />
<item Nome="Scanner N-13 " Peso="5 kg" Armazem="5" Quantidade="8" Preco="R$ 1505,00" />
<item Nome="Monitor PH-1" Peso="2 kg" Armazem="1" Quantidade="45" Preco="R$ 123,99" />
</estoque>
I've written annotated XML template classes to be read using JAXB. They looked like this:
Root.java:
import javax.xml.bind.annotation.*;
@XmlRootElement(name="estoque")
@XmlAccessorType(XmlAccessType.FIELD)
public class Raiz {
@XmlElement(name="item")
private Item[] itens;
public Item[] getItens() {
return itens;
}
}
And to Item.java:
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class Item {
@XmlAttribute
private String Nome;
@XmlAttribute
private String Peso;
@XmlAttribute
private String Armazem;
@XmlAttribute
private String Quantidade;
@XmlAttribute
private String Preco;
@Override
public String toString() {
return "Item [Nome=" + Nome + ", Peso=" + Peso + ", Armazem=" + Armazem
+ ", Quantidade=" + Quantidade + ", Preco=" + Preco + "]";
}
}
But when calling the Unmarshal method of JAXB, the return is not as expected. He even notices the amount of items (3), but they are all null:
JAXBContext jaxbContext = JAXBContext.newInstance(Raiz.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
File xml = new File("/home/worknz/estoque.xml");
Raiz raiz = (Raiz) unmarshaller.unmarshal(xml);
System.out.println(raiz.getItens().length);
for(Item i: raiz.getItens()) {
System.out.println(i);
}
Output:
3
Item [Nome=null, Peso=null, Armazem=null, Quantidade=null, Preco=null]
Item [Nome=null, Peso=null, Armazem=null, Quantidade=null, Preco=null]
Item [Nome=null, Peso=null, Armazem=null, Quantidade=null, Preco=null]
What could be happening?