所以我从肥皂服务(我无法控制)中获取了一个 xml 文件。它返回一个 xmlns,这导致了简单的 XML 问题。我正在运行一个str_replace来解决这个问题,但是现在 simpleXML 只返回一个空对象。XML结构似乎很好,没有错误只是一个空对象。
$xmlString = $client->__getLastResponse();
$feed = str_replace(' xmlns="LMSWebservice"', '', $xmlString);
$sData= simplexml_load_string($feed);
print_r($sData);
返回: simpleXMLElement Object()
str 替换之前的 XML 源是:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetUserInternalIDByPersonalIDResponse xmlns="LMSWebservice">
<GetUserInternalIDByPersonalIDResult>
<Response xmlns="">
<Timestamp date="24/10/2013" time="04:27:37" />
<User>
<UserInternalID>4907</UserInternalID>
</User>
</Response>
</GetUserInternalIDByPersonalIDResult>
</GetUserInternalIDByPersonalIDResponse>
</soap:Body>
</soap:Envelope>
str 替换后:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetUserInternalIDByPersonalIDResponse>
<GetUserInternalIDByPersonalIDResult>
<Response xmlns="">
<Timestamp date="24/10/2013" time="04:27:37" />
<User>
<UserInternalID>4907</UserInternalID>
</User>
</Response>
</GetUserInternalIDByPersonalIDResult>
</GetUserInternalIDByPersonalIDResponse>
</soap:Body>
</soap:Envelope>
任何帮助将不胜感激,这让我发疯!
----因此,如果我不摆脱命名空间属性,我会收到以下错误消息:
警告:simplexml_load_string() [function.simplexml-load-string]:实体:第 1 行:解析器警告:xmlns:URI LMSWebservice 在 serviceTest2 中不是绝对的.php在第 16 行
警告:simplexml_load_string() [function.simplexml-load-string]: LSchema"><soap:Body>
警告:simplexml_load_string() [function.simplexml-load-string]:^ in serviceTest2.php 在第 16
行
如果我尝试使用 xPath 获取 UserInternalID,它会返回一个空数组。如果您所说的是正确的,并且正确地进入了simpleXML,那么我如何访问UserInternalID节点?抱歉,这是第一次使用simpleXML,这让我感到困惑。
所以只是尝试更改 NS
$feed = str_replace('xmlns="LMSWebservice"', 'xmlns="ns:LMSWebservice"', $xmlString);
没有错误。
我为xPath尝试了这个$ID = $sData->xpath('UserInternalID');
我知道这可能是完全错误的,但我还没有尝试太多其他方法,因为它似乎一开始就没有正确地进入 simpleXML。
所以我用了$ID = $sData->xpath('//UserInternalID');回声$ID[0];
这很完美。谢谢你的帮助!
通过大量的评论和您最后的编辑,最终可以揭示问题的实际原因,xpath 表达式是错误的:
$ID = $sData->xpath('UserInternalID');
错误的是路径,这不匹配任何元素。相反,您可以使用:
$ID = (string) $xml->xpath('//UserInternalID')[0];
或者更详细:
$ID = (string) $xml->xpath('//Response/User/UserInternalID')[0];
这里的关键点是,您要为要查询的元素编写正确的路径。
完整的使用示例:
<?php
/**
* SoapClient xml return string with simpleXML
*
* @link http://stackoverflow.com/q/19556414/367456
*/
$response = <<<RESPONSE
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetUserInternalIDByPersonalIDResponse xmlns="LMSWebservice">
<GetUserInternalIDByPersonalIDResult>
<Response xmlns="">
<Timestamp date="24/10/2013" time="04:27:37" />
<User>
<UserInternalID>4907</UserInternalID>
</User>
</Response>
</GetUserInternalIDByPersonalIDResult>
</GetUserInternalIDByPersonalIDResponse>
</soap:Body>
</soap:Envelope>
RESPONSE;
$restore = libxml_use_internal_errors(TRUE);
$xml = simplexml_load_string($response);
libxml_use_internal_errors($restore);
echo $xml->xpath('//Response/User/UserInternalID')[0];
输出:
4907
在线演示:https://eval.in/57149
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($response);
libxml_clear_errors();
$xml = $doc->saveXML($doc->documentElement);
$xml = simplexml_load_string($xml);
使用这个对我有很大帮助。