Emilio Numazaki
Emilio Numazaki

Reputation: 892

Jackson XML - Deserialize from specific node

I'm new to Jackson XML and I would like to do the following:

  1. Here is the XML I'd like to convert to Java DTO:

     <s:Envelope
         xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
         <s:Body>
             <cotacaoTaxaCambioResponse
                 xmlns="http://tempuri.org/">
                 <cotacaoTaxaCambioResult
                     xmlns:a="http://schemas.datacontract.org/2004/07/Exchange"
                     xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
                     <a:TaxasCambio>
                         <a:CotacaoTaxaCambio>
                             <a:CLIENTEC>0</a:CLIENTEC>
                             <a:CLIENTEV>0</a:CLIENTEV>
                             <a:COMERCIALC>0</a:COMERCIALC>
                             <a:COMERCIALV>30</a:COMERCIALV>
                             <a:DATA>2021-12-15T00:00:00</a:DATA>
                             <a:HORARIO>1027</a:HORARIO>
                             <a:MOEDA>978</a:MOEDA>
                             <a:PARIDADEC>1</a:PARIDADEC>
                             <a:PARIDADEV>1</a:PARIDADEV>
                             <a:TURISMOC>0</a:TURISMOC>
                             <a:TURISMOV>30</a:TURISMOV>
                         </a:CotacaoTaxaCambio>
                     </a:TaxasCambio>
                     <a:serviceStatus>
                         <a:CODRETORNO>0</a:CODRETORNO>
                         <a:MENSAGEM>Sucesso</a:MENSAGEM>
                         <a:MENSAGEMEN>OK</a:MENSAGEMEN>
                         <a:NRREFERENCE>0</a:NRREFERENCE>
                     </a:serviceStatus>
                 </cotacaoTaxaCambioResult>
             </cotacaoTaxaCambioResponse>
         </s:Body>
     </s:Envelope>
    
  2. Info I need is attributes of a:CotacaoTaxaCambio node.

  3. Created this DTO

         @JacksonXmlRootElement(localName = "a:CotacaoTaxaCambio")
         public class CotacaoResDTO {
    
             @JacksonXmlProperty(localName = "a:DATA")
             private String data;
    
             @JacksonXmlProperty(localName = "a:HORARIO")
             private String hora;
    
             @JacksonXmlProperty(localName = "a:MOEDA")
             private String codMoeda;
    
             @JacksonXmlProperty(localName = "a:COMERCIALV")
             private Double valorComercial;
    
             @JacksonXmlProperty(localName = "a:TURISMOV")
             private Double valorTurismo;
         }
    
  4. If I set this subset as input, it does work:

                 <a:CotacaoTaxaCambio>
                     <a:CLIENTEC>0</a:CLIENTEC>
                     <a:CLIENTEV>0</a:CLIENTEV>
                     <a:COMERCIALC>0</a:COMERCIALC>
                     <a:COMERCIALV>30</a:COMERCIALV>
                     <a:DATA>2021-12-15T00:00:00</a:DATA>
                     <a:HORARIO>1027</a:HORARIO>
                     <a:MOEDA>978</a:MOEDA>
                     <a:PARIDADEC>1</a:PARIDADEC>
                     <a:PARIDADEV>1</a:PARIDADEV>
                     <a:TURISMOC>0</a:TURISMOC>
                     <a:TURISMOV>30</a:TURISMOV>
                 </a:CotacaoTaxaCambio>
    

But it doesn't deserialize if I set full xml as input.

Is there any configuration or anotation that I can use to archieve this?

Thanks in advance,

Upvotes: 1

Views: 1211

Answers (1)

dariosicily
dariosicily

Reputation: 4517

Unfortunately at the moment the jackson library does not support directly the SOAP protocol, but you can read the part you are interested with the XMLStreamReader class directly pointing the involved tag:

XMLInputFactory f = XMLInputFactory.newFactory();
XMLStreamReader sr = f.createXMLStreamReader(new FileInputStream(xml));
XmlMapper mapper = new XmlMapper();
sr.nextTag();
while (!sr.getLocalName().equals("CotacaoTaxaCambio")) {
   sr.nextTag();
}
CotacaoResDTO value = mapper.readValue(sr, CotacaoResDTO.class);
sr.close();

You have to modify also your CotacaoResDTO class ignoring the unknown properties :

@JsonIgnoreProperties(ignoreUnknown = true)
public class CotacaoResDTO {

    @JacksonXmlProperty(localName = "DATA")
    private String data;

    @JacksonXmlProperty(localName = "HORARIO")
    private String hora;

    @JacksonXmlProperty(localName = "MOEDA")
    private String codMoeda;

    @JacksonXmlProperty(localName = "COMERCIALV")
    private Double valorComercial;

    @JacksonXmlProperty(localName = "TURISMOV")
    private Double valorTurismo;
}

Upvotes: 1

Related Questions