Home >Backend Development >PHP Tutorial >How to Correctly Parse SOAP XML Responses with Namespaces in PHP Using simpleXML?
Parsing SOAP XML
Parsing XML with namespaces can sometimes cause confusion, especially when using simpleXML.
Problem Statement
The given SOAP XML response contains a namespace prefix for the soap elements. A code snippet in PHP is provided to parse the XML but returns an empty result.
Correct Parsing Approach
One simple solution is to strip the namespace prefixes from the XML response before passing it to simpleXML:
$clean_xml = str_ireplace(['SOAP-ENV:', 'SOAP:'], '', $soap_response); $xml = simplexml_load_string($clean_xml);
Result
Using this code, you will get the following result:
SimpleXMLElement Object ( [Body] => SimpleXMLElement Object ( [PaymentNotification] => SimpleXMLElement Object ( [payment] => SimpleXMLElement Object ( [uniqueReference] => ESDEUR11039872 [epacsReference] => 74348dc0-cbf0-df11-b725-001ec9e61285 [postingDate] => 2010-11-15T15:19:45 [bankCurrency] => EUR [bankAmount] => 1.00 [appliedCurrency] => EUR [appliedAmount] => 1.00 [countryCode] => ES [bankInformation] => Sean Wood [merchantReference] => ESDEUR11039872 ) ) ) )
This will allow you to access the payment element and its child elements as expected.
The above is the detailed content of How to Correctly Parse SOAP XML Responses with Namespaces in PHP Using simpleXML?. For more information, please follow other related articles on the PHP Chinese website!