How to convert XML with a repeated element to the Java object (JAXB)

Asked

Viewed 228 times

0

I’m having a problem converting an XML file to Java Object.

This is my XML file that I receive through Webservice:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <consultaResponse xmlns="http://tempuri.org/">
         <consultaResult>
            <INFO-XML xmlns="">
               <SOLICITACAO>
                  <TIPO>PLACA</TIPO>
                  <DADO>BMW0171</DADO>
                  <NUMERO_RESPOSTA>YY07090942S6</NUMERO_RESPOSTA>
                  <TEMPO>0.150113</TEMPO>
                  <MENSAGEM>1</MENSAGEM>
                  <HORARIO>2019-07-09 09:42:29</HORARIO>
               </SOLICITACAO>
               <RESPOSTA>
                  <PLACA>BMW0171</PLACA>
                  <CHASSI>WBAA25A4RRAA00859</CHASSI>
                  <ANO>1994/1994</ANO>
                  <MARCA_MODELO>325IA SC4 REGINO</MARCA_MODELO>
                  <SITUACAO_CHASSI>NORMAL</SITUACAO_CHASSI>
                  <ANOFABRICACAO>1994</ANOFABRICACAO>
                  <ANOMODELO>1994</ANOMODELO>
                  <COMBUSTIVEL>GASOLINA</COMBUSTIVEL>
                  <COR>PRETA</COR>
                  <RENAVAM>00618596488</RENAVAM>
                  <SEGMENTO>AUTO</SEGMENTO>
                  <SUB_SEGMENTO>AU - SEDAN GRANDE</SUB_SEGMENTO>
                  <NR_MOTOR>0035110704</NR_MOTOR>
                  <NR_CAMBIO/>
                  <NR_CARROCERIA/>
                  <NR_EIXOTRASEIRO/>
                  <QUAN_EIXOS/>                     
                  <LEILAO>
                     <LEILOEIRO>N/I</LEILOEIRO>
                     <COMITENTE/>
                     <LOTE/>
                     <DATA_LEILAO>30/07/2003</DATA_LEILAO>
                     <COND_VEICULO>INTEIRO</COND_VEICULO>
                     <SIT_CHASSI/>
                     <COND_MOTOR/>
                     <COND_CAMBIO/>
                     <COND_MECANICA/>
                     <OBS/>
                  </LEILAO>
                  <LEILAO>
                     <LEILOEIRO>N/I</LEILOEIRO>
                     <COMITENTE/>
                     <LOTE>0022A</LOTE>
                     <DATA_LEILAO>30/07/2003</DATA_LEILAO>
                     <COND_VEICULO/>
                     <SIT_CHASSI/>
                     <COND_MOTOR/>
                     <COND_CAMBIO/>
                     <COND_MECANICA/>
                     <OBS/>
                  </LEILAO>
               </RESPOSTA>
            </INFO-XML>
         </consultaResult>
      </consultaResponse>
   </soap:Body>
</soap:Envelope>

This is my JAXB class:

@Data
@XmlRootElement(name = "consultaResponse", namespace = "http://tempuri.org/")
@XmlAccessorType(XmlAccessType.FIELD)
public class ConsultaResponse {

    @XmlElement(name = "consultaResult", namespace = "http://tempuri.org/")
    private ConsultaResult result;

    @Data
    @XmlAccessorType(XmlAccessType.FIELD)
    public static class ConsultaResult {

        @XmlElement(name = "INFO-XML", namespace = "")
        private InfoXML infoXML;

        @Data
        @XmlAccessorType(XmlAccessType.FIELD)
        public static class InfoXML {

            @XmlElement(name = "SOLICITACAO")
            private Solicitacao solicitacao;

            @XmlElement(name = "RESPOSTA")
            private Resposta resposta;

            @XmlElement(name = "LEILAO")
            private List<Leilao> leilao;

            @Data
            @XmlAccessorType(XmlAccessType.FIELD)
            public static class Solicitacao {

                @XmlElement(name = "TIPO")
                private String tipo;

                @XmlElement(name = "DADO")
                private String dado;

                @XmlElement(name = "NUMERO_RESPOSTA")
                private String numeroResposta;

                @XmlElement(name = "TEMPO")
                private float tempo;

                @XmlElement(name = "MENSAGEM")
                private int mensagem;

                @XmlElement(name = "HORARIO")
                private int horario;

            }

            @Data
            @XmlAccessorType(XmlAccessType.FIELD)
            public static class Resposta {

                @XmlElement(name = "PLACA")
                private String placa;

                @XmlElement(name = "CHASSI")
                private String chassi;

                @XmlElement(name = "ANO")
                private String ano;

                @XmlElement(name = "MARCA_MODELO")
                private String marcaModelo;

                @XmlElement(name = "SITUACAO_CHASSI")
                private String situacaoChassi;

                @XmlElement(name = "ANOFABRICACAO")
                private int anoFabricacao;

                @XmlElement(name = "ANOMODELO")
                private int anoModelo;

                @XmlElement(name = "COMBUSTIVEL")
                private String combustivel;

                @XmlElement(name = "COR")
                private String cor;

                @XmlElement(name = "RENAVAM")
                private String renavam;

                @XmlElement(name = "SEGMENTO")
                private String segmento;

                @XmlElement(name = "SUB_SEGMENTO")
                private String subSegmento;

                @XmlElement(name = "NR_MOTOR")
                private String nrMotor;

                @XmlElement(name = "NR_CAMBIO")
                private String nrCambio;

                @XmlElement(name = "NR_CARROCERIA")
                private String nrCarroceria;

                @XmlElement(name = "NR_EIXOTRASEIRO")
                private String nrEixoTraseiro;

                @XmlElement(name = "QUAN_EIXOS")
                private int quantEixos;

            }

            @Data
            @XmlAccessorType(XmlAccessType.FIELD)
            public static class Leilao {

                @XmlElement(name = "LEILOEIRO")
                private String leiloeiro;

                @XmlElement(name = "COMITENTE")
                private String comitente;

                @XmlElement(name = "LOTE")
                private String lote;

                @XmlElement(name = "DATA_LEILAO")
                private String dataLeilao;

                @XmlElement(name = "COND_VEICULO")
                private String condLeilao;

                @XmlElement(name = "SIT_CHASSI")
                private String sitChassi;

                @XmlElement(name = "COND_MOTOR")
                private String condMotor;

                @XmlElement(name = "COND_CAMBIO")
                private String condCambio;

                @XmlElement(name = "COND_MECANICA")
                private String condMecanica;

                @XmlElement(name = "OBS")
                private String obs;

            }
        }
    }
}

I have a problem in this XML snippet, where there are two LEILAO elements:

  <LEILAO>...</LEILAO>
  <LEILAO>...</LEILAO>

In class I wrote these elements as a List but it returns Null:

@XmlElement(name = "LEILAO") 
private List<Leilao> leilao;

1 answer

0

After spending the day beating myself, I found where the problem was My Auction List was nestled directly in the Root (INFO-XML), in fact I just moved into the Answer and everything worked.

My class was like this:

@Data
@XmlRootElement(name = "consultaResponse", namespace = "http://tempuri.org/")
@XmlAccessorType(XmlAccessType.FIELD)
public class ConsultaResponse {

    @XmlElement(name = "consultaResult", namespace = "http://tempuri.org/")
    private ConsultaResult result;

    @Data
    @XmlAccessorType(XmlAccessType.FIELD)
    public static class ConsultaResult {

        @XmlElement(name = "INFO-XML", namespace = "")
        private InfoXML infoXML;

        @Data
        @XmlAccessorType(XmlAccessType.FIELD)
        public static class InfoXML {

            @XmlElement(name = "SOLICITACAO")
            private Solicitacao solicitacao;

            @XmlElement(name = "RESPOSTA")
            private Resposta resposta;

            @Data
            @XmlAccessorType(XmlAccessType.FIELD)
            public static class Solicitacao {

                @XmlElement(name = "TIPO")
                private String tipo;

                @XmlElement(name = "DADO")
                private String dado;

                @XmlElement(name = "NUMERO_RESPOSTA")
                private String numeroResposta;

                @XmlElement(name = "TEMPO")
                private float tempo;

                @XmlElement(name = "MENSAGEM")
                private int mensagem;

                @XmlElement(name = "HORARIO")
                private int horario;

            }

            @Data
            @XmlAccessorType(XmlAccessType.FIELD)
            public static class Resposta {

                @XmlElement(name = "PLACA")
                private String placa;

                @XmlElement(name = "CHASSI")
                private String chassi;

                @XmlElement(name = "ANO")
                private String ano;

                @XmlElement(name = "MARCA_MODELO")
                private String marcaModelo;

                @XmlElement(name = "SITUACAO_CHASSI")
                private String situacaoChassi;

                @XmlElement(name = "ANOFABRICACAO")
                private int anoFabricacao;

                @XmlElement(name = "ANOMODELO")
                private int anoModelo;

                @XmlElement(name = "COMBUSTIVEL")
                private String combustivel;

                @XmlElement(name = "COR")
                private String cor;

                @XmlElement(name = "RENAVAM")
                private String renavam;

                @XmlElement(name = "SEGMENTO")
                private String segmento;

                @XmlElement(name = "SUB_SEGMENTO")
                private String subSegmento;

                @XmlElement(name = "NR_MOTOR")
                private String nrMotor;

                @XmlElement(name = "NR_CAMBIO")
                private String nrCambio;

                @XmlElement(name = "NR_CARROCERIA")
                private String nrCarroceria;

                @XmlElement(name = "NR_EIXOTRASEIRO")
                private String nrEixoTraseiro;

                @XmlElement(name = "QUAN_EIXOS")
                private int quantEixos;

                @XmlElement(name = "LEILAO")
                private List<Leilao> leiloes = new ArrayList<Leilao>();

            }

            @Data
            @XmlAccessorType(XmlAccessType.FIELD)
            public static class Leilao {

                @XmlElement(name = "LEILOEIRO")
                private String leiloeiro;

                @XmlElement(name = "COMITENTE")
                private String comitente;

                @XmlElement(name = "LOTE")
                private String lote;

                @XmlElement(name = "DATA_LEILAO")
                private String dataLeilao;

                @XmlElement(name = "COND_VEICULO")
                private String condLeilao;

                @XmlElement(name = "SIT_CHASSI")
                private String sitChassi;

                @XmlElement(name = "COND_MOTOR")
                private String condMotor;

                @XmlElement(name = "COND_CAMBIO")
                private String condCambio;

                @XmlElement(name = "COND_MECANICA")
                private String condMecanica;

                @XmlElement(name = "OBS")
                private String obs;

            }
        }
    }
}

Browser other questions tagged

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