SimpleXML SOAP response Namespace issues

Posted by Stu on Stack Overflow See other posts from Stack Overflow or by Stu
Published on 2010-06-11T11:19:20Z Indexed on 2010/06/11 11:22 UTC
Read the original article Hit count: 361

Filed under:
|
|
|

Hi.

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

$XmlStr = <?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>

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 />";<br />
}

doesn't work. I have read a number of articles such as
http://www.sitepoint.com/blogs/2005/10/20/simplexml-and-namespaces/
htp://blog.stuartherbert.com/php/2007/01/07/using-simplexml-to-parse-rss-feeds/
and about 20 questions on this site which unfortunately are not helping.

(the second link has htp:// as a newbie here I cannot post more than one link)

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

© Stack Overflow or respective owner

Related posts about php

Related posts about Xml