-2

I have following XML response which I want to parse to an array:

$response = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<epp xmlns="http://www.google.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="dominx.xsd">
<command>
<create>
<domain:create
xmlns:domain="http://www.google.com"
xsi:schemaLocation="domain-2.0.xsd">
<domain:name>xxxx</domain:name>
<domain:ns>xxxx</domain:ns>
<domain:ns>ns1.xxxx</domain:ns>
<domain:registrant>xxxx</domain:registrant>
<domain:contact type="tech">xxxxx</domain:contact>
<domain:authInfo>
<domain:pw>xxxx</domain:pw>
</domain:authInfo>
</domain:create>
</create>
</command>
</epp>';

function object2array($object) { return @json_decode(@json_encode($object),1); };
$xml = simplexml_load_string($response);
$xml_array=object2array($xml); 

With above I'm getting empty array -> [create] => Array ( ) .

I would like to get full array in $xml_array. Is it possible to get for this XML with simplexml or should I split this XML somehow ?

pet
  • 25
  • 4
  • 2
    Your `XML is invalid`, which probably what you want to look at. – l'L'l Aug 02 '14 at 20:56
  • It is valid 100% (at least it's same as documentation from provider), and even with above xxxxxx it gets validated at: http://www.w3schools.com/xml/xml_validator.asp – pet Aug 02 '14 at 21:02
  • Sure, the syntax is fine — I'm talking about `namespace` errors though; I think that might be tripping up the output. – l'L'l Aug 02 '14 at 21:13
  • 1
    PHP Warning: simplexml_load_string(): namespace warning : xmlns: URI xxxxxx is not absolute in /tmp/test.php on line 30 – hek2mgl Aug 02 '14 at 21:14
  • I've edited code, please check now... sorry just can't show the full original links, but that should be enough to eliminate namespace warnings. – pet Aug 02 '14 at 21:30
  • Think carefully about whether that `object2array` step is actually necessary or useful. SimpleXML provides a lot of helper methods, many of which work like arrays (e.g. the ability to `foreach` over all child nodes with a particular name), but gives you much more flexibility than a plain array can. – IMSoP Aug 03 '14 at 12:43

1 Answers1

0

No you do not need to split it. You have to use ->children(), then provide the namespace. Example:

$response = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<epp xmlns="http://www.google.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="dominx.xsd">
    <command>
        <create>
            <domain:create xmlns:domain="http://www.google.com" xsi:schemaLocation="domain-2.0.xsd">
                <domain:name>xxxx</domain:name>
                <domain:ns>xxxx</domain:ns>
                <domain:ns>ns1.xxxx</domain:ns>
                <domain:registrant>xxxx</domain:registrant>
                <domain:contact type="tech">xxxxx</domain:contact>
                <domain:authInfo>
                    <domain:pw>xxxx</domain:pw>
                </domain:authInfo>
            </domain:create>
        </create>
    </command>
</epp>';

$xml = simplexml_load_string($response);
$domain = $xml->command->create->children('domain', 'http://www.google.com');
echo '<pre>';
print_r($domain);

Should outputthis:

SimpleXMLElement Object
(
    [create] => SimpleXMLElement Object
        (
            [name] => xxxx
            [ns] => Array
            (
                [0] => xxxx
                [1] => ns1.xxxx
            )

            [registrant] => xxxx
            [contact] => xxxxx
            [authInfo] => SimpleXMLElement Object
            (
                [pw] => xxxx
            )
        )
)
Kevin
  • 41,694
  • 12
  • 53
  • 70