2016-11-10 15:07 阅读 56

使用所有XMLNS解析SOAP信封 - 不在Magento上工作

I have tried and tried and have been unable to come up with a solution. My issue is this: I have a SOAP envelop response which is as follows...

    <soapenv:envelope xmlns:soapenv="">
            <mcu:prescreenusereligibilityresponse xmlns:mcu="">
                <common:response xmlns:common="">

I then access the elements like this on my mac:

$xml=new SimpleXMLElement($string);
foreach($xml->getNamespaces(true) as $key=>$url){
    $xml->registerXPathNamespace($key, $url);


Using the same method on a separate Linux instance I get an error on the print_r line, saying the last child cannot be null. I have confirmed that the values are correct. I have also tried using $xml->xpath('//mcu:eligibilitystatuscode') with no success.

I'm really stuck -_-

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    duanjiaopi8218 duanjiaopi8218 2016-11-10 16:54

    I used an alternative. DOMDocument()

    I noticed after printing out the different object nodes to the screen, the capitalization in the xml string was different. I tried comparing the namespaces with proper capitalization and still had no success using SimpleXML.

    $doc = new DOMDocument();
                    foreach($doc->getElementsByTagNameNS('*', '*') as $element){
                                return true;
                                return false;
                    return false;
                    return false;

    Above is the working code, where result is the xml returned from the soap request. My code essentially cycles through each node in the xml response. Which in turn displays on a block on the Magento front end.

    点赞 评论 复制链接分享
  • duanchao9559 duanchao9559 2016-11-10 15:22

    Considering that you say it works on your Mac and not on Linux, it is possible that the line endings are an issue. Try adding this to replace all possible line endings with the default for the system you are running on prior to parsing the XML.

    $string = preg_replace('~\R~u', PHP_EOL, $string);
    点赞 评论 复制链接分享