Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 589463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:25:37+00:00 2026-05-13T15:25:37+00:00

I’m using simpleXML to parse this xml file . It’s a feed I’m using

  • 0

I’m using simpleXML to parse this xml file. It’s a feed I’m using to access the YouTube API. I want to embed the most recent video in an object and display the next four thumbnails.

So I’m using simplexml_load_file on this, going using a foreach loop to access the values in each feed.

I can access the values no problem, but I run into the problem that it stores each video in a separate SimpleXMLElement Object. I don’t have any control over which object I’m accessing as they are not stored in an array. So I can’t get, say, $thumb[4] or $entry[4]->thumb.

I tried to use SimpleXMLIterator, but for whatever reason, any values that have the same beginning render as blank. For example, the video could have eleven variations of:

And these would each render as [1]="", [2]="", [3]="", etc.

I’ll happily provide some more information to anyone who can help!

Edit

Here is the print_r of my results. This is done on a single variable to give you an idea of the structure issue I’m facing. The entire print_r($entry) would give variables for each node in the XML file.

SimpleXMLElement Object
(
    [0] => 159
)
SimpleXMLElement Object
(
    [0] => 44
)

Also, the print_r is simply inside the PHP block for testing. I’m actually looking to access the variables within echoes in the HTML.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:25:37+00:00Added an answer on May 13, 2026 at 3:25 pm

    You ran into the problem of namespaces and SimpleXML. The feed starts with

    <feed xmlns='http://www.w3.org/2005/Atom' xmlns:media='http://search.yahoo.com/mrss/' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:gd='http://schemas.google.com/g/2005' xmlns:yt='http://gdata.youtube.com/schemas/2007'>
    

    The xmlns='http://www.w3.org/2005/Atom' sets the default namespace to http://www.w3.org/2005/Atom. I.e. its child elements are not really id, updated, category but http://www.w3.org/2005/Atom:id, http://www.w3.org/2005/Atom:updated and so on…
    So you can’t access the the id element via $feed->id, you need the method SimpleXMLELement::children(). It lets you specify the namespace of the child elements you want to retrieve.

    For example,

    $feed = simplexml_load_file('http://gdata.youtube.com/feeds/api/videos?author=ofcoursegolf&max-results=5&prettyprint=true');
    $children = $feed->children('http://www.w3.org/2005/Atom');
    echo $children->updated;
    

    Currently prints 2010-02-06T05:23:33.858Z.

    To get the id of the first entry element you can use echo $children->entry[0]->id;.
    But then you’ll hit the <media:group> element and its children <media:category, <media:player> and so on…, which are in the xmlns:media='http://search.yahoo.com/mrss/' namespace.

    $feed = simplexml_load_file('http://gdata.youtube.com/feeds/api/videos?author=ofcoursegolf&max-results=5&prettyprint=true');
    $group = $feed->children('http://www.w3.org/2005/Atom')
      ->entry[0]
      ->children('http://search.yahoo.com/mrss/')
      ->group
      ->children('http://search.yahoo.com/mrss/')
    ;
    echo $group->player->attributes()->url, "\n";
    foreach( $group->thumbnail as $thumb) {
      echo 'thumb: ', $thumb->attributes()->url, "\n";
    }
    

    (Currently) prints

    http://www.youtube.com/watch?v=ikACkCpJ-js&feature=youtube_gdata
    thumb: http://i.ytimg.com/vi/ikACkCpJ-js/2.jpg
    thumb: http://i.ytimg.com/vi/ikACkCpJ-js/1.jpg
    thumb: http://i.ytimg.com/vi/ikACkCpJ-js/3.jpg
    thumb: http://i.ytimg.com/vi/ikACkCpJ-js/0.jpg
    

    Edit: I’d probably do that within the browser with a lot more JavaScript, but here’s a (simple, ugly) example app.

    <?php
    //define('TESTENV' , true);
    function getFeed($author) {
      // <-- add caching here if needed -->
      if ( !defined('TESTENV') ) {
        $url = sprintf('http://gdata.youtube.com/feeds/api/videos?author=%s&max-results=5',
          urlencode($author)
        );
      }
      else {
        $url = 'feed.xml';
      }
      return simplexml_load_file($url);
    }
    $feed = getFeed('jonlajoie');
    
    if ( !isset($_GET['id']) ) {
      $selected = '';
    }
    else {
      $selected =  $_GET['id'];
      printf ('
        <object width="425" height="344">
          <param name="movie" value="http://www.youtube.com/v/%s"></param>
          <param name="allowFullScreen" value="true"></param>
          <param name="allowscriptaccess" value="always">
          </param>
          <embed src="http://www.youtube.com/v/%s" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed>
        </object>',
        htmlspcialchars($selected), htmlspcialchars($selected)
      );
    }
    
    $item = 0;
    foreach( $feed->children('http://www.w3.org/2005/Atom')->entry as $entry ) {
      $entryElements = $entry->children('http://www.w3.org/2005/Atom');
      $groupElements = $entry
        ->children('http://search.yahoo.com/mrss/')
        ->group
        ->children('http://search.yahoo.com/mrss/')
      ;
    
      if ( !preg_match('!^http://gdata.youtube.com/feeds/api/videos/([^/]+)!', $entryElements->id, $m) ) {
        // google can choose whatever id they want. But this is only a simple example....
        die('unexpected id: '.htmlspecialchars($entryElements->id));
      }
      $id = $m[1];
      if ( $selected!==$id ) {
        printf('<a href="?id=%s"><img src="%s" /></a>',
          urlencode($id),
          $groupElements->thumbnail[0]->attributes()->url
        );
      }
    }
    

    Edit 2: “And when I click the thumbnails, I’ll use jQuery to load the video in the main space. Seems like I’ll need precise access to node[#], right?”
    If you’re already using JavaScript/jQuery your PHP script (if needed at all) could simply return a (JSON encoded) array of all the data for all the video and your jQuery script could figure out what to do with the data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i want to parse a xhtml file and display in UITableView. what is the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an XML file, the creators of it stuck in a bunch social
I'm making a simple page using Google Maps API 3. My first. One marker

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.