SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2007
    Location
    Bilthoven, Netherlands
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    simplexml - cannot acces attribute data of node

    Below I read an xml-file (rss feed from Picasa). The media-tags are in a namespace, hence I have to access them through the children() method. And I am successful in doing that - i.e. the expression "echo (String)$media2[1];" echos the nodevalue.

    I need to get the attribute data from the nodes, though. I try to do that with i.e. the expression "echo (String)$media2[0]['url'];" - and nothing comes out of it - although the node definately has a url-attribute:
    Code:
    <media:content url='http://lh4.ggpht.com/_hyRpI70q6R4/SdSkNdfGxyI/AAAAAAAACQc/XXefthQ5WwU/DSC_0175.JPG' height='1106' width='1600' type='image/jpeg' medium='image'/>
    Can anyone see what goes wrong?

    PHP Code:
    $pic simplexml_load_file('xml/picasaphotos-all.xml');
    $media=$pic->channel->item[0]->children('http://search.yahoo.com/mrss/');
    $media2=$media->children('http://search.yahoo.com/mrss/');

    echo (String)
    $media2[0]['url'];
    echo 
    $media2[0]['height'];
    echo (String)
    $media2[1];
    echo (String)
    $media2[2]['type'];
    echo (String)
    $media2[3];
    echo (String)
    $media2[4];
    echo (String)
    $media2[5];
    echo (String)
    $media2[6]; 
    The xml-file I'm importing can be found here:
    http://ellehauge.net/xml/picasaphotos-all.xml (1.4MB)

  2. #2
    SitePoint Addict skunkbad's Avatar
    Join Date
    Apr 2008
    Location
    Temecula, CA
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    See the docs, http://www.php.net/manual/en/simplex...attributes.php :

    Code:
    <?php
    $string = <<<XML
    <a xmlns:b>
     <foo name="one" game="lonely">1</foo>
    </a>
    XML;
    
    $xml = simplexml_load_string($string);
    foreach($xml->foo[0]->attributes() as $a => $b) {
        echo $a,'="',$b,"\"\n";
    }
    ?>

  3. #3
    SitePoint Zealot
    Join Date
    Aug 2008
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $pic = simplexml_load_file('picasaphotos-all.xml');
    $media=$pic->channel->item[0]->children('http://search.yahoo.com/mrss/');
    $media2=$media->children('http://search.yahoo.com/mrss/');
    $media3 = $media2->thumbnail[0]->attributes()->url;

    echo $media3;

  4. #4
    SitePoint Zealot
    Join Date
    Oct 2007
    Location
    Bilthoven, Netherlands
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help - it works for me now like this (no thumbnail element in between):
    PHP Code:
    $thumb.="<img src='".(string)$media2[5]->attributes()->url."' alt=' ' />";
    echo 
    $thumb
    I don't understand why, though. These two expressions are supposed to be the same:
    PHP Code:
    $media2[5]->attributes()->url;
    $media2[5]['url']; 
    - according to i.e. my sitepoint book on xml page 178 - see attached image.
    Attached Images Attached Images

  5. #5
    SitePoint Zealot
    Join Date
    Aug 2008
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found the following note in php docs http://www.php.net/manual/en/simplex...t.children.php

    -----------
    Note: SimpleXML has made a rule of adding iterative properties to most methods. They cannot be viewed using var_dump() or anything else which can examine objects.
    -----------

    For me it means that simpleXML object are not completely php objects. Maybe this is a reason

  6. #6
    SitePoint Zealot
    Join Date
    Oct 2007
    Location
    Bilthoven, Netherlands
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hm, not sure I understand ..
    Either way - if the book is wrong, perhaps I should report it to the publisher.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •