douxi5940 2016-11-10 15:07
浏览 72
已采纳

使用所有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="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:header>
        <soapenv:body>
            <mcu:prescreenusereligibilityresponse xmlns:mcu="http://www.ups.com/XMLSchema/XOLTWS/MCUserEligibility/v1.0">
                <common:response xmlns:common="http://www.ups.com/XMLSchema/XOLTWS/Common/v1.0">
                    <common:responsestatus>
                        <common:code>1</common:code>
                        <common:description>Success</common:description>
                    </common:responsestatus>
                </common:response>
                <mcu:eligibilitystatuscode>3</mcu:eligibilitystatuscode>
            </mcu:prescreenusereligibilityresponse>
        </soapenv:body>
    </soapenv:header>
</soapenv:envelope>

I then access the elements like this on my mac:

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

}
print_r(strval($xml->children($ns[0])->header->body->children($ns[1])->prescreenusereligibilityresponse->eligibilitystatuscode));

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 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();
    
                $doc->loadXML($result);
                if($doc){
                    foreach($doc->getElementsByTagNameNS('*', '*') as $element){
                        if($element->tagName=='mcu:EligibilityStatusCode'){
                            if($element->nodeValue==0){
                                return true;
                            }
                            else{
                                return false;
                            }
                        }
                    }
                    return false;
                }
                else{
                    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题