Jump to content

Parsing XML and removing parent if link = string


mkrisch

Recommended Posts

Hello,

 

Any help would be great. I'm filtering and rss feed and to create a new feed with some <item>'s removed. Here is a snippet from the source file.

 

<item>

<title><h1>Indians, Pacers and Colts attendance is only part of story</h1></title>

<link>http://www.newsweek.com/the-score/2010/07/29/indians-pacers-and-colts-draw-similar-sized-crowds/PARAMS/post/21385</link>

<description></description>

</item>

 

<item>

<title><h1>Declining attendance forcing Speedway to make changes</h1></title>

 

<link>http://www.newsweek.com/declining-attendance-forcing-speedway-to-make-changes/PARAMS/article/21369</link>

<description></description>

</item>

 

<item>

<title><h1>Three issues that could derail Indy's 2012 Super Bowl</h1></title>

<link>http://www.newsweek.com/the-score/2010/07/27/three-issues-that-could-derail-indys-2012-super-bowl/PARAMS/post/21341</link>

<description></description>

</item>

 

 

I'm wanting to use the link tag to detect the item that need to be removed for the new rss feed. All <item>with <link> that contain http://www.newsweek.com/the-score/ as the beginning of the link need to be filtered out.

 

Here is my code so far.

 

<?php echo '<?xml version="1.0" encoding="UTF-8"?>

<rss version="2.0" xmlns:media = "http://search.yahoo.com/mrss">

<channel>

<title>CLE Calendar</title>

<description>CLE Calendar</description>

<language>en-us</language>

<pubDate>June 14, 2010</pubDate>

<lastBuildDate>June 14, 2010</lastBuildDate>

<ttl>15</ttl>'; ?>

 

 

<?php $rss = new SimpleXMLElement('http://www.newsweek.com/mobile-app/rss/feed?rssId=18&premium=false', null, true); ?>

 

<?php foreach ($rss->channel->item as $items): ?>

 

<item>

<title><?php echo '<h1>' . $items->title . '</h1>'; ?></title>

<link><?php echo $items->link; ?></link>

</item>

<?php endforeach; ?>

</channel>

</rss>

 

Thanks in advance,

MK

 

Link to comment
Share on other sites

Change

<?php foreach ($rss->channel->item as $items): ?>

 

To

<?php foreach ($rss->channel->item as $items): 
    // skip entry if link begins with http://www.newsweek.com/the-score
    if(substr($items->link, 0, 33) == 'http://www.newsweek.com/the-score')
        continue;
?>

 

Link to comment
Share on other sites

Thanks for the quick reply. Would you have a look and tell me where I went wrong. The <item> is still there after code modification.

 

<?php echo '<?xml version="1.0" encoding="UTF-8"?>

<rss version="2.0" xmlns:media = "http://search.yahoo.com/mrss">

<channel>

<title>Sports Business</title>

<description>Sports Business</description>

<language>en-us</language>

<pubDate>June 14, 2010</pubDate>

<lastBuildDate>June 14, 2010</lastBuildDate>

<ttl>15</ttl>'; ?>

 

 

<?php $rss = new SimpleXMLElement('http://www.ibj.com/mobile-app/rss/feed?rssId=18&premium=false', null, true); ?>

 

<?php foreach ($rss->channel->item as $items):

    // skip entry if link begins with http://www.newsweek.com/the-score

    if(substr($items->link, 0, 33) == 'http://www.ibj.com/the-score')

        continue;

?>

 

<item>

<title><?php echo '<h1>' . $items->title . '</h1>'; ?></title>

<link><?php echo $items->link; ?></link>

<description><?php //echo $items->description; ?></description>

</item>

 

<?php endforeach; ?>

</channel>

</rss>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.