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

2 Answers 2

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 
Sign up to request clarification or add additional context in comments.

2 Comments

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 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.
0

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

$xml = preg_replace('%<(\/)?(\S+):(\w+)%', '<$1$3', $xml); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.