1

I am getting complex XML as a response i need to convert in to an array, here is my sample complex XML

   <?xml version="1.0" encoding="UTF-8"?>
 <Response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <SearchResult>
  <Result>
     <aaa>string</aaa>
     <bbb>string</bbb>
     <ccc>
        <ddd>
           <eee>string</eee>
           <fff>string</fff>
        </ddd>
        <ddd>
           <eee>string</eee>
           <fff>string</fff>
        </ddd>
     </ccc>
     <ggg>
        <hhh>
           <iii>string</iii>
           <eee>string</eee>
           <jjj>string</jjj>

        </hhh>
        <hhh>
           <iii>string</iii>
           <eee>string</eee>
           <jjj>string</jjj>

        </hhh>
     </ggg>
     <kkk>
        <hhh>
           <iii>string</iii>
           <eee>string</eee>
           <jjj>string</jjj>

        </hhh>
     </kkk>
     <lll>
        <mmm>string</mmm>
        <nnn>string</nnn>
        <ooo>string</ooo>

     </lll>

  </Result>


    <Result>
   //same values as above 
    </Result>



 <Result>
   //same values as above 
   </Result>

    </SearchResult> 
 </Response>

I have tried simple XML but its not working,

    function xml2Array($xmlstring)
{
    $xml = simplexml_load_string($xmlstring, "SimpleXMLElement", LIBXML_NOCDATA);
    $json = json_encode($xml);
    return json_decode($json,TRUE);
}

$arr = xml2Array($response);
print_r($arr);

But it returns empty array please suggest with possible solution for this

vellai durai
  • 1,019
  • 3
  • 16
  • 38

1 Answers1

0

As a parameter pass a XML format data and after that it converts to array.

function xml2Array($xmlstring)
{

    $xml = simplexml_load_string($xmlstring, "SimpleXMLElement", LIBXML_NOCDATA);
    $json = json_encode($xml);
    return json_decode($json,TRUE);
}

$arr = xml2Array($response);
print_r($arr);