1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How do I get Namespaces with SimpleXML?

Discussion in 'XenForo Development Discussions' started by Jaxel, Nov 7, 2010.

  1. Jaxel

    Jaxel Well-Known Member

    Okay... lets say I have the following XML feed:
    Code:
    <?xml version='1.0' encoding='UTF-8'?>
    <entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:gd="http://schemas.google.com/g/2005" xmlns:yt="http://gdata.youtube.com/schemas/2007">
        <id>http://gdata.youtube.com/feeds/api/videos/kCVZv-cPC10</id>
        <published>2009-04-02T22:58:20.000Z</published>
        <updated>2010-08-27T06:19:27.000Z</updated>
        <title type="text">Soul Edge/Blade: Introduction: Edge of Soul</title>
        <author>
            <name>Jaxelrod</name>
            <uri>http://gdata.youtube.com/feeds/api/users/jaxelrod</uri>
        </author>
        <media:group>
            <media:title type="plain">Soul Edge/Blade: Introduction: Edge of Soul</media:title>
            <media:description type="plain">This introduction was done on the PSX...</media:description>
            <media:keywords>Soul, Edge, Blade, Calibur, Soulcalibur, Team, Kogarasumaru, 8WayRun.com</media:keywords>
            <media:thumbnail url="http://i.ytimg.com/vi/kCVZv-cPC10/2.jpg" height="90" width="120" time="00:01:06.500" />
            <media:thumbnail url="http://i.ytimg.com/vi/kCVZv-cPC10/1.jpg" height="90" width="120" time="00:00:33.250" />
            <media:thumbnail url="http://i.ytimg.com/vi/kCVZv-cPC10/3.jpg" height="90" width="120" time="00:01:39.750" />
            <media:thumbnail url="http://i.ytimg.com/vi/kCVZv-cPC10/0.jpg" height="240" width="320" time="00:01:06.500" />
            <yt:duration seconds="133" />
        </media:group>
    </entry>
    If I parse this feed with SimpleXMLElement, it returns:
    Code:
    SimpleXMLElement Object
    (
        [id] => http://gdata.youtube.com/feeds/api/videos/kCVZv-cPC10
        [published] => 2009-04-02T22:58:20.000Z
        [updated] => 2010-08-27T06:19:27.000Z
        [title] => Soul Edge/Blade: Introduction: Edge of Soul
    )
    As you can see, I'm not retrieving any namespaced information... it doesn't parse media:group or any of it's children. How do I get access to this namespace? Basically I want to be able to do the following:

    $title = $xml->media:group->media:title;
    $duration = $xml->media:group->yt:duration->children('seconds');
     
  2. Shadab

    Shadab Well-Known Member

    This blogpost has a nice explanation on how to retrieve namespaced elements in xml:
    » http://blog.sherifmansour.com/?p=302

    For your feed, try this:
    PHP:
    $xml = new SimpleXMLElement($data);
    $namespaces $xml->getNamespaces(true);

    $media $xml->children($namespaces['media']);
    $yt $media->group->children($namespaces['yt']);

    $duration $yt->duration->attributes();

    print_r(array(
        
    $media->group->title,
        
    $media->group->description,
        
    $media->group->keywords,
        
    $duration['seconds']
    ));
     
  3. Jaxel

    Jaxel Well-Known Member

    Thanks... this is what I am using now...

    Code:
    $atom = new SimpleXMLElement($service['service_feed'], null, true);
    $ns = $atom->getNameSpaces(true);
    
    $titl = $atom->children($ns['media'])->group->title;
    $desc = $atom->children($ns['media'])->group->description;
    $keyw = $atom->children($ns['media'])->group->keywords;
    $dura = $atom->children($ns['media'])->group->children($ns['yt'])->duration->attributes()->seconds;
    $thum = $atom->children($ns['media'])->group->thumbnail[4]->attributes()->url;
    
     
  4. Jaxel

    Jaxel Well-Known Member

    is there a shorthand way of doing all this stuff? it seems a little cumbersome and unwieldy...
     
  5. Shadab

    Shadab Well-Known Member

    Not that I know of, unfortunately.
    At least it's a lot more easier than manually parsing the feed or using regex to extract the data. :)
     
  6. Mike

    Mike XenForo Developer Staff Member

    Regex XML parsing. Bad Shadab! ;)
     
  7. Jaxel

    Jaxel Well-Known Member

    Someone like me... who has a college degree in Regular Expressions (Biomedical Informatics)... I tend to fall back on RegEx way too often...
     
  8. Vladislav Rastrusny

    Vladislav Rastrusny Active Member

    Why to parse RSS manually if we have Simplepie?
     
  9. Jaxel

    Jaxel Well-Known Member

    Okay... I am having problems parsing an XML file...

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <nicovideo_thumb_response status="ok">
        <thumb>
            <video_id>sm11944073</video_id>
            <title>【VF5FS】アオイ対戦動画3回目 vsラウ 2試合分</title>
            <description>どうも、テストも兼ねての投稿3回目です。あんまり上手くないですが、適当に見てやってください。タイトルをちょっと変更、今回は2試合分をくっつけてうpしました。1試合目は野良対戦、2試合目は知り合いとの対戦です。※アオイだけじゃなくて、ベネ・アキラ・ウルフ辺りを使ってたりもします。実力称号A止まりの下手糞動画でも、見たい方とかいらっしゃいますか?そもそもVF5FSの動画自体があんまり上がって無いので需要とかよくわかんないです( ・ω・)前 sm11864383 vsゴウ次 sm12253425VF5FS対戦動画マイリスト⇒mylist/20737080</description>
            <thumbnail_url>http://tn-skr2.smilevideo.jp/smile?i=11944073</thumbnail_url>
            <first_retrieve>2010-09-01T01:30:49+09:00</first_retrieve>
            <length>6:37</length>
            <movie_type>mp4</movie_type>
            <size_high>76597940</size_high>
            <size_low>15515837</size_low>
            <view_counter>693</view_counter>
            <comment_num>29</comment_num>
            <mylist_counter>5</mylist_counter>
            <last_res_body>l;; エコノミーだと見てら 今のとこppk入るよ 4以来の葵使いですん </last_res_body>
            <watch_url>http://www.nicovideo.jp/watch/sm11944073</watch_url>
            <thumb_type>video</thumb_type>
            <embeddable>1</embeddable>
            <no_live_play>0</no_live_play>
            <tags domain="jp">
                <tag category="1" lock="1">ゲーム</tag>
                <tag>VF5FS</tag>
                <tag>アオイ</tag>
            </tags>
            <user_id>5359233</user_id>
        </thumb>
    </nicovideo_thumb_response>
    I can get all the information I need, except the tags. I am trying to get the tags, and implode them into a comma seperated list... the following is NOT working...
    Code:
    implode(",", $mrss->thumb->tags->tag);
     
  10. Indigo

    Indigo Active Member

    I believe it's because you're dealing with a Traversable rather than an array.
    Try:
    PHP:
    implode(","iterator_to_array($mrss->thumb->tags->tag));
     

Share This Page