Home > Software design >  How convert xml with namespaces to php array
How convert xml with namespaces to php array

Time:08-29

I Can't figure out how to convert this xmlstring to php array despite reading many posts.

SimpleXML does not work, creates only a empty object.

My target is to álways extract the first <value xmlns="">1.01655E1</value> found.

CODE: $arr = new SimpleXmlElement($xml); print_r($arr);

RESULT: SimpleXMLElement Object ( )

XML:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
    <SOAP-ENV:Body>
        <ns0:getInterestAndExchangeRatesResponse xmlns:ns0="http://swea.riksbank.se/xsd">
            <return xmlns="">
                <datefrom xmlns="">2022-08-04</datefrom>
                <dateto xmlns="">2022-08-10</dateto>
                <groups xmlns="">
                    <groupid xmlns="">130</groupid>
                    <groupname xmlns="">Currencies against Swedish kronor</groupname>
                    <series xmlns="">
                        <seriesid xmlns="">SEKUSDPMI</seriesid>
                        <seriesname xmlns="">1 USD</seriesname>
                        <unit xmlns="">1.0E0</unit>
                        <resultrows xmlns="">
                            <date xmlns="">2022-08-04</date>
                            <period xmlns:ns1="http://www.w3.org/2001/XMLSchema-instance" xmlns="" ns1:nil="true"/>
                            <min xmlns:ns1="http://www.w3.org/2001/XMLSchema-instance" xmlns="" ns1:nil="true"/>
                            <average xmlns:ns1="http://www.w3.org/2001/XMLSchema-instance" xmlns="" ns1:nil="true"/>
                            <max xmlns:ns1="http://www.w3.org/2001/XMLSchema-instance" xmlns="" ns1:nil="true"/>
                            <ultimo xmlns:ns1="http://www.w3.org/2001/XMLSchema-instance" xmlns="" ns1:nil="true"/>
                            <value xmlns="">1.01997E1</value>
                        </resultrows>
                        <resultrows xmlns="">
                            <date xmlns="">2022-08-05</date>
                            <period xmlns:ns1="http://www.w3.org/2001/XMLSchema-instance" xmlns="" ns1:nil="true"/>
                            <min xmlns:ns1="http://www.w3.org/2001/XMLSchema-instance" xmlns="" ns1:nil="true"/>
                            <average xmlns:ns1="http://www.w3.org/2001/XMLSchema-instance" xmlns="" ns1:nil="true"/>
                            <max xmlns:ns1="http://www.w3.org/2001/XMLSchema-instance" xmlns="" ns1:nil="true"/>
                            <ultimo xmlns:ns1="http://www.w3.org/2001/XMLSchema-instance" xmlns="" ns1:nil="true"/>
                            <value xmlns="">1.01279E1</value>
                        </resultrows>
                        <resultrows xmlns="">
                            <date xmlns="">2022-08-08</date>
                            <period xmlns:ns1="http://www.w3.org/2001/XMLSchema-instance" xmlns="" ns1:nil="true"/>
                            <min xmlns:ns1="http://www.w3.org/2001/XMLSchema-instance" xmlns="" ns1:nil="true"/>
                            <average xmlns:ns1="http://www.w3.org/2001/XMLSchema-instance" xmlns="" ns1:nil="true"/>
                            <max xmlns:ns1="http://www.w3.org/2001/XMLSchema-instance" xmlns="" ns1:nil="true"/>
                            <ultimo xmlns:ns1="http://www.w3.org/2001/XMLSchema-instance" xmlns="" ns1:nil="true"/>
                            <value xmlns="">1.01655E1</value>
                        </resultrows>
                        <resultrows xmlns="">
                            <date xmlns="">2022-08-09</date>
                            <period xmlns:ns1="http://www.w3.org/2001/XMLSchema-instance" xmlns="" ns1:nil="true"/>
                            <min xmlns:ns1="http://www.w3.org/2001/XMLSchema-instance" xmlns="" ns1:nil="true"/>
                            <average xmlns:ns1="http://www.w3.org/2001/XMLSchema-instance" xmlns="" ns1:nil="true"/>
                            <max xmlns:ns1="http://www.w3.org/2001/XMLSchema-instance" xmlns="" ns1:nil="true"/>
                            <ultimo xmlns:ns1="http://www.w3.org/2001/XMLSchema-instance" xmlns="" ns1:nil="true"/>
                            <value xmlns="">1.01517E1</value>
                        </resultrows>
                        <resultrows xmlns="">
                            <date xmlns="">2022-08-10</date>
                            <period xmlns:ns1="http://www.w3.org/2001/XMLSchema-instance" xmlns="" ns1:nil="true"/>
                            <min xmlns:ns1="http://www.w3.org/2001/XMLSchema-instance" xmlns="" ns1:nil="true"/>
                            <average xmlns:ns1="http://www.w3.org/2001/XMLSchema-instance" xmlns="" ns1:nil="true"/>
                            <max xmlns:ns1="http://www.w3.org/2001/XMLSchema-instance" xmlns="" ns1:nil="true"/>
                            <ultimo xmlns:ns1="http://www.w3.org/2001/XMLSchema-instance" xmlns="" ns1:nil="true"/>
                            <value xmlns="">1.0173E1</value>
                        </resultrows>
                    </series>
                </groups>
            </return>
        </ns0:getInterestAndExchangeRatesResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

This is getting frustrating ;) Big thanks to anyone who can help!

CodePudding user response:

I'm still not sure what the issue is exactly, but with this:

$arr = new SimpleXMLElement($xml);
$target = ($arr->xpath('//*[local-name()="value"]')[0]);
echo($target);

you can get

1.01997E1

and if you're interested in, for example, the third <value> element, it's just:

$target = ($arr->xpath('//*[local-name()="value"]')[2]);
echo($target);

which returns

1.01655E1
  • Related