1

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;
    }
Sharpzain120
  • 365
  • 3
  • 9
  • 18

2 Answers2

0

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";
    }
}
Woody
  • 5,052
  • 2
  • 22
  • 28
  • Maybe you could add what it was you wanted to extract? I agree that it would be easier to look with XPath and do a loop through all media nodes, picking the information you want, but without knowing what you want the output to be, it is hard to say – Woody Oct 23 '11 at 19:26
  • I want to extract album and subalbum but with proper hierarchy? – Sharpzain120 Oct 23 '11 at 19:31
  • it doesnt traverse the array.It show the same album name and its attributes four times – Sharpzain120 Oct 23 '11 at 20:15
  • Your answer is right wood just a small change that please chane $album to $subalbum – Sharpzain120 Oct 23 '11 at 20:46
0

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";
}
jsleuth
  • 638
  • 4
  • 9