4

Possible Duplicate:
How to parse SOAP response without SoapClient

I have a simple nuSoap XML response:

<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <LoginResult xmlns="http://Siddin.ServiceContracts/2006/09">FE99E5267950B241F96C96DC492ACAC542F67A55</LoginResult> </soap:Body> </soap:Envelope> 

Now I'm trying to parse it with simplexml_load_string as suggested here: parse an XML with SimpleXML which has multiple namespaces and here: Trouble Parsing SOAP response in PHP using simplexml, but I can't get it working.

This is my code:

$xml = simplexml_load_string( $this->getFullResponse() ); $xml->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/'); $xml->registerXPathNamespace('xsi', 'http://www.w3.org/2001/XMLSchema-instance'); $xml->registerXPathNamespace('xsd', 'http://www.w3.org/2001/XMLSchema'); foreach($xml->xpath('//soap:Body') as $header) { var_export($header->xpath('//LoginResult')); } 

But I still get only this in result:

/* array ( ) 

What am I doing wrong? Or what simple thing am I missing to understand?


The working end result with DOM by MySqlError:

$doc = new DOMDocument(); $doc->loadXML( $response ); echo $doc->getElementsByTagName( "LoginResult" )->item(0)->nodeValue; 

The working end result with SimpleXML by ndm:

$xml = simplexml_load_string( $response ); foreach($xml->xpath('//soap:Body') as $header) { echo (string)$header->LoginResult; } 
3

2 Answers 2

15
$doc = new DOMDocument(); $doc->loadXML( $yourxmlresponse ); $LoginResults = $doc->getElementsByTagName( "LoginResult" ); $LoginResult = $LoginResults->item(0)->nodeValue; var_export( $LoginResult ); 
Sign up to request clarification or add additional context in comments.

2 Comments

I'm not getting any results out of this
if you change var_export to echo it will return without ' mark var_export is just showing you that it's string
5

What's going wrong here is SimpleXMLs poor default namespace support. In order to fetch that node using an XPath expression, you'd need to register a prefix for the default namespace and use it in the query, even though the element isn't prefixed, for example:

foreach($xml->xpath('//soap:Body') as $header) { $header->registerXPathNamespace('default', 'http://Siddin.ServiceContracts/2006/09'); var_export($header->xpath('//default:LoginResult')); } 

However, actually there's no need to use XPath for accessing this node, you can simply access it directly:

foreach($xml->xpath('//soap:Body') as $header) { var_export($header->LoginResult); } 

2 Comments

This seems to work in some way, but gives me string like this, witch is still not exactly what I need: SimpleXMLElement::__set_state(array( 0 => 'FE99E5267950B241F96C96DC492ACAC542F67A55', ))
Well, I've adapted your example code, ofcourse that's how a var_export of a SimpleXML node looks like, if you need to access the value, simply cast the node to a string: (string)$header->LoginResult

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.