SimpleXML not pulling correct link

Posted by kylex on Stack Overflow See other posts from Stack Overflow or by kylex
Published on 2010-04-13T20:28:37Z Indexed on 2010/04/13 20:33 UTC
Read the original article Hit count: 448

Filed under:
|

My code does not pull the proper link for the item. Instead it's getting the previous item's link.

Any suggestions? Thanks!

<?php
// Load the XML data from the specified file name.  
// The second argument (NULL) allows us to specify additional libxml parameters,
// we don't need this so we'll leave it as NULL.  The third argument however is
// important as it informs simplexml to handle the first parameter as a file name
// rather than a XML formatted string.
$pFile = new SimpleXMLElement("http://example.blogspot.com/feeds/posts/default?alt=rss", null, true);  

// Now that we've loaded our XML file we can begin to parse it.
// We know that a channel element should be available within the,
// document so we begin by looping through each channel
foreach ($pFile->channel as $pChild)
{    

    // Now we want to loop through the items inside this channel
    {

    foreach ($pFile->channel->item as $pItem)
    {

        // If this item has child nodes as it should, 
        // loop through them and print out the data


        foreach ($pItem->children() as $pChild)
        { 
            // We can check the name of this node using the getName() method.
            // We can then use this information, to, for example, embolden
            // the title or format a link
            switch ($pChild->getName())
            {
                case 'pubDate':
                    $date = date('l, F d, Y', strtotime($pChild));
                    echo "<p class=\"blog_time\">$date</p>"; 
                    break;

                case 'link':
                    $link = $pChild;
                    break;

                case 'title':

                    echo "<p class=\"blog_title\"><a href=\"$link\">$pChild</a></p>";
                    break;

               case 'description':
                    // echo substr(strip_tags($pChild), 0 , 270) . "...";
                    break;



                case 'author':
                    echo "";
                    break;    
                case 'category':
                    echo "";
                    break;
                case 'guid':
                    echo "";
                    break;

                default:
                    // echo strip_tags($pChild) . "<br />\n";
                    break;
            }

        }

   }


   }

}

?> 

© Stack Overflow or respective owner

Related posts about simplexml

Related posts about php