SimpleXML SOAP response Namespace issues

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

Leave a Comment