本文介紹了在 PHP SimpleXML 對象上使用 xpath,SOAP + 命名空間(不工作..)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
在 SO 和 google 上研究了幾個小時之后......我希望在這里得到一些幫助:(我離運行正則表達式以完全刪除命名空間僅一步之遙)
After researching this on SO and google for hours now... I hope to get some help here: (I am just one step away from running a regex to remove the namespaces completely)
首先這是 XML:
<?xml version="1.0" encoding="utf-16"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header xmlns="http://webservices.site.com/definitions">
<SessionId>0119A|1</SessionId>
</soap:Header>
<soap:Body>
<Security_AuthenticateReply xmlns="http://xml.site.com/QQ">
<processStatus>
<statusCode>P</statusCode>
</processStatus>
</Security_AuthenticateReply>
</soap:Body>
</soap:Envelope>
現在這是我在 PHP 中的代碼的樣子:
Now this is what my code in PHP looks like:
$response = simplexml_load_string( $str ,NULL,
false, "http://schemas.xmlsoap.org/soap/envelope/" );
// just making sure the name space is "registered"
// but I tested all examples also with this removed
$response->registerXPathNamespace("soap",
"http://schemas.xmlsoap.org/soap/envelope/");
$_res = $response->xpath('//soap:Header');
print_r($_res);
/*** result: simple query for the root "soap" namespace, this looks good! (so far..)
Array
(
[0] => SimpleXMLElement Object
(
[SessionId] => 0119A|1
)
)
***/
// now we query for the "SessionId" element in the XML
$_res = $response->xpath('//soap:Header/SessionId');
print_r($_res);
/*** result: this does not return anything!
Array
(
)
***/
// another approach
$_res = $response->xpath('//soap:Header/SessionId/text()');
print_r($_res);
/*** result: this does not return anything at all!
***/
// Finally, without using XPath this does work
$_res = $response->xpath('//soap:Header');
$_res = (string)$_res[0]->SessionId;
echo $_res;
/*** result: this worked
0119A|1
***/
如何使用 XPATH 獲取 SOAP 消息???
How can I get the SOAP message working with XPATH???
謝謝,羅馬
推薦答案
多個命名空間都搞砸了,給我添加以下作品
The multiple namespaces are messing with it, adding the following works for me
$response->registerXPathNamespace("site", "http://webservices.site.com/definitions");
$_res = $response->xpath('//site:SessionId');
另請參閱此之前的堆棧溢出問題
also, see this previous stack overflow question
這篇關于在 PHP SimpleXML 對象上使用 xpath,SOAP + 命名空間(不工作..)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!