Posted By

abbey on 02/20/10


Tagged

object rss array xml atom XPath parsing


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

redstorm
mohamadfikri


PHP: Creating an array of objects to store atom rss data


 / Published in: PHP
 

i could put a demo of this in action if you want

  1. <?php
  2. error_reporting(E_ALL); // shows all errors, remove this on live sites
  3.  
  4. $feedURL = 'http://mydomain.com/rss/example.xml';
  5. $getFILE = get_content($feedURL) or die("feed not loading");
  6. $xml = simplexml_load_string($getFILE, NULL, LIBXML_NOCDATA); // Set CDATA as text nodes
  7. $xml->registerXPathNamespace('atom', 'http://www.w3.org/2005/Atom'); // define namespace atom
  8.  
  9. class oEntry
  10. {
  11. public $url;
  12. public $author;
  13. public $date;
  14. public $text;
  15.  
  16. function oEntry($url, $author, $date, $text) {
  17. $this->url = $url;
  18. $this->author = $author;
  19. $this->date = $date;
  20. $this->text = $text;
  21. }
  22. }
  23.  
  24. foreach($xml->xpath('atom:entry') as $entry) {
  25. // array of objects
  26. $e[] = new oEntry($xml->link['href'], $entry->author->name, $entry->published, $entry->content);
  27. } // end foreach
  28.  
  29. ?>
  30.  
  31. SAMPLE XML INPUT (you need to fill out the tags):
  32. <?xml version="1.0" encoding="UTF-8"?>
  33. <feed xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0">
  34. <link rel="alternate" type="text/html" href="" />
  35. <id></id>
  36. <updated></updated>
  37. <title></title>
  38.  
  39. <entry>
  40. <id></id>
  41. <thr:in-reply-to ref="" type="text/html" href=""/>
  42. <link rel="alternate" type="text/html" href="" />
  43. <title></title>
  44. <author>
  45. <name></name>
  46. <uri></uri>
  47. </author>
  48. <content type="html" xml:lang="en-us" xml:base="">
  49. <![CDATA[<p>html text goes here</p>]]>
  50. </content>
  51. <published>February 20, 2010 2:23 AM</published>
  52. </entry>
  53.  
  54. </feed>

Report this snippet  

You need to login to post a comment.