SoapClient XML使用simpleXML返回字符串

时间:2013-10-24 03:32:53

标签: php xml simplexml

所以我从肥皂服务中获取了一个xml文件(我无法控制)。它返回一个导致simpleXML问题的xmlns。我正在运行str_replace来摆脱这个问题,但是现在simpleXML只返回一个空对象。 XML结构看起来很好,没有错误只是一个空对象。

$xmlString = $client->__getLastResponse();
$feed = str_replace(' xmlns="LMSWebservice"', '', $xmlString);
$sData= simplexml_load_string($feed);

print_r($sData);

返回:SimpleXMLElement对象()

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]: serviceTest2.php 在线 16

警告:simplexml_load_string()[function.simplexml-load-string]:^ serviceTest2.php 16

如果我尝试使用xPath获取UserInternalID,则返回一个空数组。如果您所说的是正确的并且这正确地进入了SimpleXML,那么我该如何访问UserInternalID节点?对不起,这是第一次使用simpleXML,这让我很困惑。

所以只是尝试更改NS

$ feed = str_replace('xmlns =“LMSWebservice”','xmlns =“ns:LMSWebservice”',$ xmlString);

进入没有错误。

我为xPath尝试了这个 $ ID = $ sData-&gt; xpath('UserInternalID');

我知道这可能是完全错误的,但是我没有尝试过其他的东西,因为它似乎没有正确地进入simpleXML。 :/

所以我用过 $ ID = $ sData-&gt; xpath('// UserInternalID'); echo $ ID [0];

完美无缺。谢谢你的帮助!

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($response);
libxml_clear_errors();
$xml = $doc->saveXML($doc->documentElement);
$xml = simplexml_load_string($xml);

使用这对我帮助很大。

相关问题