SimpleXML SOAP response Namespace issues

前端 未结 2 467
野性不改
野性不改 2020-11-29 10:44

After spending SEVERAL frustrated hours on this I am asking for your help.

I am trying to get the content of particular nodes from a SOAP response.

The resp

相关标签:
2条回答
  • 2020-11-29 11:11

    A cheap and nasty way is to just simply remove the prefixes:

    $xml = preg_replace('%<(\/)?(\S+):(\w+)%', '<$1$3', $xml);
    
    0 讨论(0)
  • 2020-11-29 11:26

    You have to use SimpleXMLElement::children(), though at this point it would probably be easier to use XPath.

    <?php
        $XmlStr = <<<XML
    <?xml version="1.0" encoding="UTF-8"?>
    <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"  xmlns:ns1="http://soap.xxxxxx.co.uk/" >
        <env:Body>
            <ns1:PlaceOrderResponse>
                <xxxxxOrderNumber></xxxxxOrderNumber>
                <ErrorArray>
                    <Error>
                        <ErrorCode>24</ErrorCode>
                        <ErrorText>The+client+order+number+3002254+is+already+in+use</ErrorText>
                    </Error>
                    <Error>
                        <ErrorCode>1</ErrorCode>
                        <ErrorText>Aborting</ErrorText>
                    </Error>
                </ErrorArray>
            </ns1:PlaceOrderResponse>
        </env:Body>
    </env:Envelope>
    XML;
    
        $XmlArray   = new SimpleXMLElement($XmlStr);
    
        $t = $XmlArray->children("env", true)->Body->
            children("ns1", true)->PlaceOrderResponse->
            children()->ErrorArray->Error;
        foreach ($t as $error) {
            echo $error->ErrorCode, " " , $error->ErrorText, "<br />";
        } 
    

    gives:

    24 The+client+order+number+3002254+is+already+in+use
    1 Aborting
    
    0 讨论(0)
提交回复
热议问题