6

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 response is

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="<a href="http://www.w3.org/2003/05/soap-envelope">http://www.w3.org/2003/05/soap-envelope</a>"<xmlns:ns1="<a href="http://soap.xxxxxx.co.uk/">http://soap.xxxxxx.co.uk/</a>">
    <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>

I am trying to get at the nodes and children of <ErrorArray>. Because of the XML containing namespaces

$XmlArray   = new SimpleXMLElement($XmlStr);

foreach ($XmlArray->env:Envelope->env:Body->ns1:PlaceOrderResponse->ErrorArray->Error as $Error)
{
    echo $Error->ErrorCode."<br />";
}

doesn't work. I have read a number of articles such as

and about 20 questions on this site, which unfortunately are not helping.

Even writing,

$XmlArray   = new SimpleXMLElement($XmlStr);

echo "<br /><br /><pre>\n";
print_r($XmlArray);
echo "<pre><br /><br />\n";

gives

SimpleXMLElement Object
(
)

which makes me wonder if the soap response ($XmlStr) is actually a valid input for SimpleXMLElement.

It seems that the line

$XmlArray   = new SimpleXMLElement($XmlStr);

is not doing what I expect it to.

Any help on how to get the nodes from the XML above would be very welcome.

Obviously getting it to work (having a working example) is what I need in the short term, but if someone could help me understand what I am doing wrong would be better in the long term.

Cheers. Stu

Artefacto
  • 96,375
  • 17
  • 202
  • 225
Stu
  • 63
  • 1
  • 1
  • 3

2 Answers2

17

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
hakre
  • 193,403
  • 52
  • 435
  • 836
Artefacto
  • 96,375
  • 17
  • 202
  • 225
  • THANK YOU!, I was so close. I had already looking into the children() method and was using this foreach ($XmlArray->children('env',true)->Envelope->children('env',true)->Body->children('ns1',true)->PlaceOrderResponse->ErrorArray->Error as $Error) Again, thank you :-) P.s. what are the tags to format the code correctly on stackoverflow? I couldn't find it on the side. – Stu Jun 11 '10 at 12:19
  • @Stu You just have to press the 0101 button with your code selected. By the way, if the answer was indeed satisfactory, you should accept it. – Artefacto Jun 11 '10 at 12:35
0

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

$xml = preg_replace('%<(\/)?(\S+):(\w+)%', '<$1$3', $xml);
Danny Kopping
  • 4,862
  • 2
  • 29
  • 38