Error reading XML with JAXB: all null items after Unmarshal

Asked

Viewed 286 times

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?

1 answer

0

The error is very simple but difficult to detect. XML is capitalized (Name, Weight, Store, etc).

In the Java class, even putting variables with names in upper case 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. Swap 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 exit 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]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.