JAXB - Tag repeating, instead of nesting

Asked

Viewed 181 times

6

I have a problem that I can not store more than one element in the same TAG, look at 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 "Obsproducao" TAG repeats instead of being 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 class that repeats:

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

            protected String sDsObservacaoProducao;
......

1 answer

4


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

@XmlElementWrapper(name = "ObsProducao")
@XmlElement(name = "sDsObservacaoProducao")
protected List<String> obsProducao;

The @XmlElementWrapper will generate a wrapper called ObsProducao, the @XmlElement will cause each item within the list to generate an element called sDsObservacaoProducao, soon, the result will be what you expect:

<ObsProducao>
    <sDsObservacaoProducao>Com Gelo</sDsObservacaoProducao>
    <sDsObservacaoProducao>Sem Açucar</sDsObservacaoProducao>
</ObsProducao>
  • Anthony worked perfectly :) ... but how would the XSD of this XML file look, because the way I created the XSD, when the classes from it the result of the XML is the one with the tag repeating, take a look at my xsd, please:

  • <xsd:element name="Itemcardapio" minOccurs="1" maxOccurs="unbounded" > <xsd:complexType> <xsd:Quence> <xsd:element name="nCdItemCardapio" type="xsd:int" /> <xsd:element name="nQtdeItemCardapio" type="xsd:int" /> <xsd:element name="Obsproducao" minOccurs="0" maxOccurs="unbounded" > <xsd:complexType> <xsd:Sequence> <xsd:element name="sDsObservedProduction" type="xsd:string" minOccurs="0" /> </xsd:Sequence> </xsd:complexType> </xsd:element> </xsd:Sequence> </xsd:complexType> </xsd:element>

  • You can use the scheming to generate an xsd of your class and see how Java does this construction. Anyway, if I was going to generate an XSD in hand (overall I avoid that) I would use a xsd:complexType to the list (containing a string sequence unbouded whose name is sDsObservacaoProducao). The ItemCardapio would have a xsd:element called ObsProducao referencing this complex type.

  • Did it work Diego? If yes please accept the answer.

  • Gave yes, thanks friend :)

Browser other questions tagged

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