第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Java中的XPath查詢(xún)不返回任何元素

Java中的XPath查詢(xún)不返回任何元素

瀟湘沐 2022-06-15 17:21:14
我正在開(kāi)發(fā)一個(gè)使用 SOAP 作為通信協(xié)議的 Web 服務(wù):我通過(guò)使用 Spring Boot 和 Spring WS 來(lái)實(shí)現(xiàn)這一點(diǎn)。有時(shí),我可能需要通過(guò)請(qǐng)求的標(biāo)頭發(fā)送一些內(nèi)容,并且我希望能夠通過(guò) XPath 表達(dá)式恢復(fù)該信息。這是我到目前為止所做的:        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());在下面的代碼片段中,我閱讀了獲取后端接收的整個(gè)請(qǐng)求。之后,我以前所做的是將所有這些轉(zhuǎn)換為一個(gè) SOAPHeader 對(duì)象,該對(duì)象包含我想要的所有內(nèi)容......除了我根本無(wú)法進(jìn)行任何 XPath 查詢(xún)以及我真正需要的元素下樹(shù)。因此,我決定以下列方式繼續(xù)該代碼片段        //-------        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();        DocumentBuilder builder = factory.newDocumentBuilder();        Document doc = builder.parse(new java.io.ByteArrayInputStream(payload.getBytes()));        XPath xpath = XPathFactory.newInstance().newXPath();現(xiàn)在,這是我發(fā)送的請(qǐng)求示例<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-這是我的問(wèn)題:Java 端的 XPath 表達(dá)式不返回任何內(nèi)容,而其他工具(例如這個(gè))正確評(píng)估表達(dá)式并返回一些內(nèi)容。在 Java 中唯一能正確計(jì)算的 XPath 表達(dá)式是 //*,僅此而已。我錯(cuò)過(guò)了什么?
查看完整描述

2 回答

?
米琪卡哇伊

TA貢獻(xiàn)1998條經(jīng)驗(yàn) 獲得超6個(gè)贊

我知道上次我在 java 中使用 XPath 時(shí)遇到了命名空間問(wèn)題,必須添加一個(gè) namespaceContext 才能使其工作。


如果要在 XPath 中使用soapenv 命名空間,則需要“注冊(cè)”它。你可以在下面找到我當(dāng)時(shí)所做的事情。可能不是 100% 干凈,但仍可能對(duì)您有所幫助


    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;

        }

    });

編輯:用我對(duì)您的代碼的修復(fù)進(jìn)行了測(cè)試運(yùn)行,這是您期望看到的嗎?


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


查看完整回答
反對(duì) 回復(fù) 2022-06-15
?
動(dòng)漫人物

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超10個(gè)贊

這是使一切正常工作所缺少的唯一代碼行


        factory.setNamespaceAware(true);

所以整個(gè)工作代碼是


        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;


查看完整回答
反對(duì) 回復(fù) 2022-06-15
  • 2 回答
  • 0 關(guān)注
  • 109 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)