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:
    <?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">
        <title type="text">Soul Edge/Blade: Introduction: Edge of Soul</title>
            <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" />
    If I parse this feed with SimpleXMLElement, it returns:
    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:
    $xml = new SimpleXMLElement($data);
    $namespaces $xml->getNamespaces(true);

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

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

  3. Jaxel

    Jaxel Well-Known Member

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

    $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...

    <?xml version="1.0" encoding="UTF-8"?>
    <nicovideo_thumb_response status="ok">
            <title>【VF5FS】アオイ対戦動画3回目 vsラウ 2試合分</title>
            <description>どうも、テストも兼ねての投稿3回目です。あんまり上手くないですが、適当に見てやってください。タイトルをちょっと変更、今回は2試合分をくっつけてうpしました。1試合目は野良対戦、2試合目は知り合いとの対戦です。※アオイだけじゃなくて、ベネ・アキラ・ウルフ辺りを使ってたりもします。実力称号A止まりの下手糞動画でも、見たい方とかいらっしゃいますか?そもそもVF5FSの動画自体があんまり上がって無いので需要とかよくわかんないです( ・ω・)前 sm11864383 vsゴウ次 sm12253425VF5FS対戦動画マイリスト⇒mylist/20737080</description>
            <last_res_body>l;; エコノミーだと見てら 今のとこppk入るよ 4以来の葵使いですん </last_res_body>
            <tags domain="jp">
                <tag category="1" lock="1">ゲーム</tag>
    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...
    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.

Share This Page