1

I need to filter and reorder an RSS feed.

Using PHP, how would I detect if there is a link in the enclosure url="" ?

Most of these will be empty, but I'd like to move the whole item that has a link in this tag to the top.

Here is an example of the code I'll be working with. I'm guessing I would use isset? Something like:

if (isset($enclosure)){
    HOW WOULD I SELECT THE PARENT ITEM? AND EVERYTHING IN IT? AND THEN MOVE IT TO THE TOP?
}

<item>
    <title><![CDATA[Article title 1]]></title>
    <link><![CDATA[http://www.articlelinkone.com]]></link>
    <pubDate><![CDATA[Tue, 26 Feb 2013 15:45:00 EDT]]></pubDate>
    <description><![CDATA[Article discription 1<br />Some HTML will exist]]></description>
    <enclosure url="" type="image/jpeg"></enclosure>
    <thumbnail url="" type="image/jpeg"></thumbnail>
    <summary><![CDATA[Summary for article]]></summary>
    <guid><![CDATA[Guide for article]]></guid>
    <source url="http://www.article.com/"/>
</item> 
<item>
    <title><![CDATA[Article title 1]]></title>
    <link><![CDATA[http://www.articlelinkone.com]]></link>
    <pubDate><![CDATA[Tue, 26 Feb 2013 15:45:00 EDT]]></pubDate>
    <description><![CDATA[Article discription 1<br />Some HTML will exist]]></description>
    <enclosure url="" type="image/jpeg"></enclosure>
    <thumbnail url="" type="image/jpeg"></thumbnail>
    <summary><![CDATA[Summary for article]]></summary>
    <guid><![CDATA[Guide for article]]></guid>
    <source url="http://www.article.com/"/>
</item> 
<item>
    <title><![CDATA[Article title 1]]></title>
    <link><![CDATA[http://www.articlelinkone.com]]></link>
    <pubDate><![CDATA[Tue, 26 Feb 2013 15:45:00 EDT]]></pubDate>
    <description><![CDATA[Article discription 1<br />Some HTML will exist]]></description>
    <enclosure url="http://articleone.com/image1.jpg" type="image/jpeg"></enclosure>
    <thumbnail url="http://articleone.com/thumb/image1.jpg" type="image/jpeg"></thumbnail>
    <summary><![CDATA[Summary for article]]></summary>
    <guid><![CDATA[Guide for article]]></guid>
    <source url="http://www.article.com/"/>
</item> 
<item>
    <title><![CDATA[Article title 1]]></title>
    <link><![CDATA[http://www.articlelinkone.com]]></link>
    <pubDate><![CDATA[Tue, 26 Feb 2013 15:45:00 EDT]]></pubDate>
    <description><![CDATA[Article discription 1<br />Some HTML will exist]]></description>
    <enclosure url="" type="image/jpeg"></enclosure>
    <thumbnail url="" type="image/jpeg"></thumbnail>
    <summary><![CDATA[Summary for article]]></summary>
    <guid><![CDATA[Guide for article]]></guid>
    <source url="http://www.article.com/"/>
</item> 
CosminO
  • 5,018
  • 6
  • 28
  • 50
mkrisch
  • 105
  • 2
  • 12

3 Answers3

3

Here is how you can check if the enclosure URL is not empty with SimpleXML :

<?php
$xml = new new SimpleXMLElement($xml_content)
foreach( $xml->item AS $item ) {
    if( !empty( $item->enclosure['url'] ))
        // Do whatever you want
}

Depending on what you want to do, you can put the $item in a new array, build a new XML file, etc.

Arthur
  • 3,717
  • 7
  • 28
  • 44
  • Using this function, how would I select the whole parent node and move to the top? – mkrisch Feb 27 '13 at 21:01
  • The whole node is in `$item` (because `$xml->item` is an array with all found in the XML file). You can set it at the top of another XML file. You should [take a look here](http://stackoverflow.com/questions/143122/using-simplexml-to-create-an-xml-object-from-scratch) to see how to create an XML file with SimpleXML. – Arthur Feb 28 '13 at 09:06
2

Have you looked into SimpleXML? You'll be able to easily reverse the items within the xml object with something like:

$str = file_get_contents($url);
$xml = simplexml_load_string($str);
$items = array_reverse($xml->xpath('item'));
Malachi
  • 33,142
  • 18
  • 63
  • 96
1

I think you really should look at php dom xml

Simple Example of DOM XML:

<?php
$xmlDoc = new DOMDocument();
$xmlDoc->load("note.xml");

$x = $xmlDoc->documentElement;
foreach ($x->childNodes AS $item)
  {
  print $item->nodeName . " = " . $item->nodeValue . "<br>";
  }
?> 

You may read the full tutorial on W3Schools (recommended)

Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100
Oliver M Grech
  • 3,071
  • 1
  • 21
  • 36
  • If I'm understanding this correctly, I would bring the xml into the DOM, manipulate it and then export the manipulated data back to an xml file? I've been trying to get this working with the xml data I posted above. Is this what you would do? http://www.w3schools.com/dom/tryit.asp?filename=try_dom_clonenode – mkrisch Feb 27 '13 at 21:30
  • Yes that's it my friend. While SimpleXML is simpler to use, DOMXML is much much more powerful, especially to parse large files. You can start with SimpleXML as specified by the users above and then if you require further power, you may switch to DOMXML. As explained previously DOMXML and SimpleXML are interchangable so code working with SimpleXML will work with DOMXML. Always try to use OOP concepts with regards to this subject as it makes your life easier if you decide to switch :) – Oliver M Grech Feb 28 '13 at 08:08