JAXB - Tag repeating, instead of nesting

6

I have a problem that I can not store more than one element in the same TAG, look at the XML:

<ItemCardapio>
    <nCdItemCardapio>10</nCdItemCardapio>
    <nQtdeItemCardapio>2</nQtdeItemCardapio>
    <ObsProducao>
        <sDsObservacaoProducao>Com Gelo</sDsObservacaoProducao>
    </ObsProducao>
    <ObsProducao>
        <sDsObservacaoProducao>Sem Açucar</sDsObservacaoProducao>
    </ObsProducao>
</ItemCardapio>

As you can see, the TAG "ObsProducao" repeats, instead of looking like this:

<ItemCardapio>
    <nCdItemCardapio>10</nCdItemCardapio>
    <nQtdeItemCardapio>2</nQtdeItemCardapio>
    <ObsProducao>
        <sDsObservacaoProducao>Com Gelo</sDsObservacaoProducao>
        <sDsObservacaoProducao>Sem Açucar</sDsObservacaoProducao>
    </ObsProducao>
</itemcardapio>

Follow the class with JAXB annotations:

@XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "nCdItemCardapio",
        "nQtdeItemCardapio",
        "obsProducao"
    })
    public static class ItemCardapio {

        protected int nCdItemCardapio;
        protected int nQtdeItemCardapio;
        @XmlElement(name = "ObsProducao")
        protected List<WsTSPaymentParamPedido.ItemCardapio.ObsProducao> obsProducao;
            ..........

Now the repeating class:

 @XmlAccessorType(XmlAccessType.FIELD)
        @XmlType(name = "", propOrder = {
            "sDsObservacaoProducao"
        })
        public static class ObsProducao {

            protected String sDsObservacaoProducao;
......
    
asked by anonymous 01.03.2014 / 01:22

1 answer

4

Since your ObsProducao class is just a wrapper for a String , you can completely eliminate it:

@XmlElementWrapper(name = "ObsProducao")
@XmlElement(name = "sDsObservacaoProducao")
protected List<String> obsProducao;
The @XmlElementWrapper will generate a wrapper named ObsProducao , the @XmlElement will cause each item within the list to generate an element called sDsObservacaoProducao , so the result will be what you wait:

<ObsProducao>
    <sDsObservacaoProducao>Com Gelo</sDsObservacaoProducao>
    <sDsObservacaoProducao>Sem Açucar</sDsObservacaoProducao>
</ObsProducao>
    
01.03.2014 / 01:56