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> 

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Using this function, how would I select the whole parent node and move to the top?
The whole <item> node is in $item (because $xml->item is an array with all <item> found in the XML file). You can set it at the top of another XML file. You should take a look here to see how to create an XML file with SimpleXML.
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'));

1 Comment

+1 Also... The SimpleXML is interchangable with DOMXML (posted below with a tutorial as well).
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)

2 Comments

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? w3schools.com/dom/tryit.asp?filename=try_dom_clonenode
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 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.