reading Data from Parsed XML

134 Views Asked by At

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;
    }
2

There are 2 best solutions below

4
On BEST ANSWER

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";
    }
}
1
On

It would appear you have two levels of album tags. It would probably be easier not to use simpleXMLToArray() but just search for your media with xpath:

$result= $xml->xpath('//media');

while(list( , $node) = each($result)) {
    echo '/media: ',$node,"\n";
}