0
I have a simple XML stock 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 created the annotated XML template classes to be read using JAXB. They look like this:
Java root.:
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 the 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 by calling JAXB’s Unmarshal method, the return is not what you’d expect. He even notices the amount of items (3), but they’re all null and void:
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);
}
Exit:
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 might be going on?