如何将带有重复元素的XML转换为Java对象(JAXB)

时间:2019-07-09 17:35:35

标签: java xml soap jaxb

我在将重复的XML元素转换为对象时遇到问题。 我正在通过SOAP Web服务获取此XML。 这是XML:

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

这是我的JAXB课:

@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;

            }
        }
    }
}

在本节中我有一个问题,其中有两个XML的LEILAO元素:

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

在java类中,List类似于List,但是此值为Null:

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

控制台中没有出现任何错误,并且其他属性正确填充。

1 个答案:

答案 0 :(得分:0)

使用xml2xpath脚本从XML示例中打印XPath,可以看出LIELAO是RESPOSTA的子代。
因此,private List<Leilao> leilao;应该是Resposta的属性,而Leilao类的定义应该是Resposta的内部类。

./xml2xpath.sh -s '//INFO-XML/RESPOSTA' -x ~/tmp/test.xml 
Found XPath:

//RESPOSTA
//RESPOSTA/PLACA
<!-- more xpaths here -->
//RESPOSTA/LEILAO
//RESPOSTA/LEILAO/LEILOEIRO
<!-- more xpaths here -->
//RESPOSTA/LEILAO/OBS