使用xpath从soap服务中获取响应(错误:前缀必须解析为一个命名空间:soap)

问题描述 投票:0回答:1

我正在使用apache camel消费一个肥皂服务。我收到的响应如下。

INFO 17:49:59.662 - 
   <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>zzzzzz</bairro>
                     <cep>000000</cep>
                     <cidade>tttttt</cidade>
                     <complemento2>yyyyy</complemento2>
                     <end>xxxxxxxx</end>
                     <uf>wwwww</uf>
                 </return>
          </ns2:consultaCEPResponse>
     </soap:Body>
</soap:Envelope>

我试图只获取返回标签中的内容。然而,我得到以下错误。

前缀必须解析到一个命名空间:ns2

但是当我把前缀改成ns2时,它要求前缀是soap。

我应该怎么做才能得到返回标签里面的内容?

这是我的代码。

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")
            .transform().xpath("/soap:Envelope/soap:Body/ns2:consultaCEPResponse/return/text()",  
                    new Namespaces("soap","http://schemas.xmlsoap.org/soap/envelope"))
            .log("${body}");
        }
    });

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

}

java xpath soap apache-camel
1个回答
1
投票

你可能应该在你的XPath表达式中添加缺少的命名空间声明。类似于:

transform().xpath("/soap:Envelope/soap:Body/ns2:consultaCEPResponse/return//text()",  
                    new Namespaces("soap", "http://schemas.xmlsoap.org/soap/envelope/")
        .add("ns2", "http://cliente.bean.master.sigep.bsb.correios.com.br/"))

如果这还不行,也可以用.NET的方式来实现。

transform().xpath("//*[local-name()='return']//text()")

注意:我已经添加了一个 / 到你的XPath表达式 (//text()),因为没有文本是直接从返回元素的后裔。

© www.soinside.com 2019 - 2024. All rights reserved.