为了账号安全,请及时绑定邮箱和手机立即绑定

Java中的XPath查询不返回任何元素

Java中的XPath查询不返回任何元素

潇湘沐 2022-06-15 17:21:14
我正在开发一个使用 SOAP 作为通信协议的 Web 服务:我通过使用 Spring Boot 和 Spring WS 来实现这一点。有时,我可能需要通过请求的标头发送一些内容,并且我希望能够通过 XPath 表达式恢复该信息。这是我到目前为止所做的:        ByteArrayOutputStream buffer = new ByteArrayOutputStream();        WebServiceMessage message = messageContext.getRequest();        Source s = messageContext.getRequest().getPayloadSource();        messageContext.getRequest().writeTo(buffer);        String payload = buffer.toString(java.nio.charset.StandardCharsets.UTF_8.name());        System.out.println(payload);        InputStream is = new ByteArrayInputStream(payload.getBytes());在下面的代码片段中,我阅读了获取后端接收的整个请求。之后,我以前所做的是将所有这些转换为一个 SOAPHeader 对象,该对象包含我想要的所有内容......除了我根本无法进行任何 XPath 查询以及我真正需要的元素下树。因此,我决定以下列方式继续该代码片段        //-------        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();        DocumentBuilder builder = factory.newDocumentBuilder();        Document doc = builder.parse(new java.io.ByteArrayInputStream(payload.getBytes()));        XPath xpath = XPathFactory.newInstance().newXPath();现在,这是我发送的请求示例<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:it="it.niuma.mscsoapws.ws">   <soapenv:Header>       <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">       <wsse:UsernameToken wsu:Id="UsernameToken-3967AEB46D733EF6E2154990461080350">       <wsse:Username>TAVI</wsse:Username>       <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">password</wsse:Password>       <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-这是我的问题:Java 端的 XPath 表达式不返回任何内容,而其他工具(例如这个)正确评估表达式并返回一些内容。在 Java 中唯一能正确计算的 XPath 表达式是 //*,仅此而已。我错过了什么?
查看完整描述

2 回答

?
米琪卡哇伊

TA贡献1998条经验 获得超6个赞

我知道上次我在 java 中使用 XPath 时遇到了命名空间问题,必须添加一个 namespaceContext 才能使其工作。


如果要在 XPath 中使用soapenv 命名空间,则需要“注册”它。你可以在下面找到我当时所做的事情。可能不是 100% 干净,但仍可能对您有所帮助


    xpath.setNamespaceContext(new NamespaceContext() {

        @Override

        public String getNamespaceURI(String prefix) {

            if("soapenv".equals(prefix)){

                return "http://schemas.xmlsoap.org/soap/envelope/";

            }else{

                return null;

            }

        }


        @Override

        public String getPrefix(String namespaceURI) {

            return null;

        }


        @Override

        public Iterator getPrefixes(String namespaceURI) {

            return null;

        }

    });

编辑:用我对您的代码的修复进行了测试运行,这是您期望看到的吗?


found node -> Header (namespace: http://schemas.xmlsoap.org/soap/envelope/)


查看完整回答
反对 回复 2022-06-15
?
动漫人物

TA贡献1815条经验 获得超10个赞

这是使一切正常工作所缺少的唯一代码行


        factory.setNamespaceAware(true);

所以整个工作代码是


        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        factory.setNamespaceAware(true);

        DocumentBuilder builder = factory.newDocumentBuilder();

        Document doc = builder.parse(new java.io.ByteArrayInputStream(payload.getBytes()));

        XPath xpath = XPathFactory.newInstance().newXPath();

        xpath.setNamespaceContext(new NamespaceContext() {

            @Override

            public String getNamespaceURI(String prefix) {

                if("soapenv".equals(prefix)){

                    return "http://schemas.xmlsoap.org/soap/envelope/";

                }else{

                    return null;

                }

            }


            @Override

            public String getPrefix(String namespaceURI) {

                return null;

            }


            @Override

            public Iterator getPrefixes(String namespaceURI) {

                return null;

            }

        });

        XPathExpression expr = xpath.compile("//soapenv:Envelope//soapenv:Header//text()[normalize-space()]");

        Object result = expr.evaluate(doc, XPathConstants.NODESET);

        NodeList nodes = (NodeList) result;


查看完整回答
反对 回复 2022-06-15
  • 2 回答
  • 0 关注
  • 183 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号