Home >Backend Development >PHP Tutorial >How to Correctly Parse SOAP XML Responses with Namespaces in PHP Using simpleXML?

How to Correctly Parse SOAP XML Responses with Namespaces in PHP Using simpleXML?

Linda Hamilton
Linda HamiltonOriginal
2024-12-05 05:08:10758browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn