简体   繁体   中英

Get values ​from an xml using xpath in apache camel

I am trying to retrieve values from a soap service response, that is coming like this:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <ns2:consultaCEPResponse xmlns:ns2="http://cliente.bean.master.sigep.bsb.correios.com.br/"> 
             <return>
                 <bairro>Jardim das Acácias</bairro>
                 <cep>04707900</cep>
                 <cidade>São Paulo</cidade>
                 <complemento2></complemento2>
                 <end>Avenida Roque Petroni Júnior 1089</end>
                 <uf>SP</uf>
              </return>
        </ns2:consultaCEPResponse>
     </soap:Body>
</soap:Envelope>

I'm using apache camel for it.However, when I apply xpath to use only what is between the return tag, it brings everything on a single line with no tag, and for this reason, I cannot set properties

What am I getting:

INFO 15:23:47.808 - Jardim das Acácias04707900São PauloAvenida Roque Petroni Júnior 1089SP

What I want to receive:

<bairro>Jardim das Acácias</bairro>
<cep>04707900</cep>
<cidade>São Paulo</cidade>
<complemento2></complemento2>
<end>Avenida Roque Petroni Júnior 1089</end>
<uf>SP</uf>

Then after, do something like:

.setProperty("bairro",xpath("/bairro//text()"))

This is my code:

public class RotaEnviaPedidos {
public static void main(String[] args) throws Exception {
    CamelContext context = new DefaultCamelContext();
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("file:pedidos?noop=true")
            .setHeader(Exchange.HTTP_METHOD, simple("POST"))
            .to("https://apps.correios.com.br/SigepMasterJPA/AtendeClienteService/AtendeCliente")
            .convertBodyTo(String.class)
            .transform().xpath("//*[local-name()='return']//text()")
            .log("${body}");
        }
    });

    context.start();
    Thread.sleep(2000000000);
    context.stop();
}

}

PS : Maybe it is due to the use of Then do something like:

Maybe it is due to the use of '.convertBodyTo (String.class)' But if I don't use this command, it brings the following error:

 org.apache.camel.TypeConversionException: Error during type conversion from type: java.lang.String to the required type: org.w3c.dom.Document with value [Body is instance of java.io.InputStream] due org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 202; Invalid byte 2 of 3-byte UTF-8 sequence.

can you try the below:

@Override

   public void configure() throws Exception {
        from("file:pedidos?noop=true")
        .setHeader(Exchange.HTTP_METHOD, simple("POST"))
        .to("https://apps.correios.com.br/SigepMasterJPA/AtendeClienteService/AtendeCliente")
        .convertBodyTo(String.class)
        .transform().xpath("//*[local-name()='return']")
        .log("${body}");
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM