Error reading XML with JAXB: all null items after Unmarshal

0

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?

    
asked by anonymous 10.03.2017 / 03:58

1 answer

0

The error is very simple, but difficult to detect. XML has attributes in upper case (Name, Weight, Store, etc).

In the Java class, even putting variables with names in uppercase is not enough. There are two options:

1. Explicitly annotate with the XMLAttribute "name" property:

@XmlAttribute(name="Nome")
private String Nome;

@XmlAttribute(name="Peso")
private String Peso;

@XmlAttribute(name="Armazem")
private String Armazem;

@XmlAttribute(name="Quantidade")
private String Quantidade;

@XmlAttribute(name="Preco")
private String Preco;

2. Change XML attributes to lowercase, and also in Java:

@XmlAttribute
private String nome;

@XmlAttribute
private String peso;

@XmlAttribute
private String armazem;

@XmlAttribute
private String quantidade;

@XmlAttribute
private String preco;

and

<?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>

In both cases, the output will be the same:

3
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]
    
10.03.2017 / 03:58