亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 112 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號