简体   繁体   中英

SOAP envelop parsing with all XMLNS - not working on 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 -_-

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM