Search code examples
phpxmlarraysxml-parsingphotobucket

reading Data from Parsed XML


I have an XML file that is returned by an API.i want to get specfic Data out of it.I had converted the XML into array by $xml = simpleXMLToArray(simplexml_load_string($response)); $response is the XML.$xml is the array that have all the XML data.I want to extract data out of it but its not working.

foreach($xml['conetent']['album']['media'] as $media)
    {
        //var_dump($media);
      echo $media;
    }

Solution

  • It works fine here once you correct the spelling of 'conetent' to 'content'

    Edit:

    So with the update from your question, how about this:

    foreach ($xml->xpath('//content/album') as $album)
    {
        print "Album: ";
        foreach($album->attributes() as $key => $value)
            print "\t".$key." = ".$value."\n";
        foreach($album->xpath('album') as $subalbum)
        {
            print "\tSub album:\n";
            foreach($subalbum->attributes() as $key => $value)
                print "\t\t".$key." = ".$value."\n";
        }
    }