Code that works: AbsXmlRss

Code that works: AbsXmlRss

Another entry in the “Code that works” series, this time I’ll introduce you the AbsXmlRss package, which contains 4 classes:

  • AbsAtomReader10
  • AbsAtomWriter10
  • AbsRssReader20
  • AbsRssWriter20

They were designed to parse or create RSS 2.0 and ATOM 1.0 xml feed files and can successfully be used on every type of website to generate xml feed files or to display their content.

All classes’ methods are public so if you want to add extra functionality just extend the class and add your own methods. There are only two static properties defined(marked as protected), one which is present in all classes and the other one only in the AbsAtomReader10 and AbsRssReader20 classes. These are:

/*!
@ This variable holds a reference to the DOMDocument class
@ used to parse the loaded xml document
*/
protected static $_doc;
/*!
@ This variable will have the boolean value TRUE
@ if the xml document has been loaded, otherwise FALSE
*/
protected static $_loaded;

AbsAtomReader10

Use this class to parse the content of an Atom 1.0 feed

Protected Properties

/*!
@ This variable holds a reference to the DOMDocument class
@ used to parse the loaded xml document
*/
protected static $_doc;
/*!
@ This variable will have the boolean value TRUE
@ if the xml document has been loaded, otherwise FALSE
*/
protected static $_loaded;

Public Methods

public function Load( $filePath ){}
public function GetBaseTags(){}
public function GetEntries( $maxLimit = 0 ){}
public function GetAll(){}
public function Load( $filePath )

Loads the specified xml feed and assigns the content to the $_doc variable. $filePath can either be a local feed or a remote feed. You should cache it if your blog’s feed gets too many hits.

public function GetBaseTags()

This function selects from the $_doc variable the base tags and returns the result as an associated array. Base tags are those tags that are not included in the <entry> tag.

public function GetEntries( $maxLimit = 0 )

This function selects from the $_doc variable all the entries found in the <entry> tags and returns them as an associated array unless a max limit is specified.

To retrieve only a limit number of results you should specify that number, like this:

// Get only the first 2 entries
$entries = $xml->GetEntries( 2 );
public function GetAll()

This function selects everything from the $_doc variable and returns the result as an associated array.

class AbsAtomWriter10

Use this class to create an Atom 1.0 feed

Protected Properties

/**
* Holds the feed's content
* @type string
*/
protected static $_doc;

Public Methods

public function StartDocument( $xmlStylesheetFile = '' ){}
public function AddNamespaces( $xmlns = array() ){}
public function AddChannelTags( array $channelTags ){}
public function AddItems( $itemTags = array() ){}
public function EndDocument(){}
public function Display(){}
public function GetDocument(){}
public function SaveDocument( $dirPath, $fileName ){}
public function StartDocument( $xmlStylesheetFile = ” )

Starts the xml document. The optional argument is the path to the xml stylesheet file.

require( "../classes/class.AbsRssWriter20.php" );
 
$xml = new AbsRssWriter20();
 
// START DOCUMENT
$xml->StartDocument('xsl_stylesheet.xsl');

Will result to:

  <?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type="text/xsl" href="xsl_stylesheet.xsl"?><rss version="2.0"
public function AddNamespaces( $xmlns = array() )

This function adds the provided namespaces to the document. These are optional.

// ADD NAMESPACES
$ns = array(
   'slash' => 'http://purl.org/rss/1.0/modules/slash/'
  ,'content' => 'http://purl.org/rss/1.0/modules/content/'
  ,'wfw' => 'http://wellformedweb.org/CommentAPI/'
  ,'dc' => 'http://purl.org/dc/elements/1.1/'
);
$xml->AddNamespaces($ns);

Will result to:

  xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
  xmlns:content="http://purl.org/rss/1.0/modules/content/"
  xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  >
public function AddChannelTags( array $channelTags )

This function adds the provided channel tags.

// ADD CHANNEL TAGS
$channelTags = array(
   'title' => 'Latest entries on: Blog Name Here'
  ,'link' => 'http://blog-url-here/'
  ,'description' => "Coding is fun!"
  ,'pubDate' => 'Mon, 10 Apr 2009 22:00:40'
  ,'generator' => 'Blog Name Here'
  ,'language' => 'en-us'
  ,'dc:publisher' => 'Costin Trifan'
  ,'copyright' => 'Copyright (c) 2008 Costin Trifan. All rights reserved. blah blah..
);
$xml->AddChannelTags($channelTags);

Will result to:

<channel>
<title>Latest entries on: Blog Name Here</title>
<link>http://blog-url-here/</link>
<description>Coding is fun!</description>
<pubDate>Mon, 10 Apr 2009 22:00:40</pubDate>
<generator>Blog Name Here</generator>
<language>en-us</language>
<dc:publisher>Costin Trifan</dc:publisher>
<copyright>Copyright (c) 2008 Costin Trifan. All rights reserved. blah blah..</copyright>
public function AddItems( $itemTags = array() )

This function adds the provided item tags to the document. The $itemTags parameter should be provided as a bidimensional array.

// ADD ENTRIES // A bidimensional array is required!
$itemTags = array(
  array(
     'title' => 'Post 1'
    ,'link' => 'http://blog-url-here/post.php?pid=11'
    ,'description' => "<![CDATA[Posts's short description goes here]]>"
    ,'slash:comments' => 40
    ,'comments' => 'http://blog-name-here/post.php?pid=11#comments'
    ,'pubDate' => 'Sun, 12 Apr 2009 02:01:52 GMT'
    ,'category' => 'PHP'
  ),
  array(
     'title' => 'Post Title 2'
    ,'link' => 'http://blog-url-here/post.php?pid=10'
    ,'description' => "<![CDATA[Posts's short description goes here]]>"
    ,'slash:comments' => 120
    ,'comments' => 'http://blog-name-here/post.php?pid=10#comments'
    ,'pubDate' => 'Sun, 11 Apr 2009 02:01:52 GMT'
    ,'category' => 'WEB'
  )
);
$xml->AddItems($itemTags);

Will result to:

<item>
  <title>Post 1</title>
  <link>http://blog-url-here/post.php?pid=11</link>
  <description><![CDATA[Posts's short description goes here]]></description>
  <slash:comments>40</slash:comments>
  <comments>http://blog-name-here/post.php?pid=11#comments</comments>
  <pubDate>Sun, 12 Apr 2009 02:01:52 GMT</pubDate>
  <category>PHP</category>
</item>
 
<item>
  <title>Post 2</title>
  <link>http://blog-url-here/post.php?pid=10</link>
  <description><![CDATA[Posts's short description goes here]]></description>
  <slash:comments>120</slash:comments>
  <comments>http://blog-name-here/post.php?pid=10#comments</comments>
  <pubDate>Sun, 11 Apr 2009 02:01:52 GMT</pubDate>
  <category>WEB</category>
</item>

Note that you should escape special characters before adding them into the document.

public function EndDocument()

This function adds the closing document’s tags.

// END DOCUMENT
$xml->EndDocument();

Will result to:

  </channel></rss>
public function Display()

This function will display the generated xml feed.

// DISPLAY CONTENT
$xml->Display();
public function GetDocument()

This function returns the content of the document.

// GET DOCUMENT
$content = $xml->GetDocument();

Now, the $content variable will contain the generated xml document;

public function SaveDocument( $dirPath, $fileName )

This function will save the generated xml feed into the specified file($fileName) in the directory($dirPath).

// SAVE THE FEED'S CONTENT INTO AN XML FILE
try {
  if ($xml->SaveDocument(getcwd(),'rss_2.xml')) {
    exit('The feed has been successfully created!');
  }
}
catch(Exception $ex) { exit($ex->getMessage()); }

class AbsRssReader20

Use this class to parse the content of an RSS 2.0 feed

Protected Properties

/*!
@ This variable holds a reference to the DOMDocument class
@ used to parse the loaded xml document
*/
protected static $_doc;
/*!
@ This variable will have the boolean value TRUE
@ if the xml document has been loaded, otherwise FALSE
*/
protected static $_loaded;

Public Methods

public function Load( $filePath ){}
public function GetChannelTags(){}
public function GetItems( $maxLimit = 0 ){}
public function GetAll(){}
public function Load( $filePath )

Loads the specified xml feed and assigns the content to the $_doc variable. $filePath can either be a local feed or a remote feed. You should cache it if your blog’s feed gets too many hits.

public function GetChannelTags()

This function selects from the $_doc variable the channel tags and returns the result as an associated array.

public function GetItems( $maxLimit = 0 )

This function selects from the $_doc variable all the entries found in the <item> tags and returns them as an associated array unless a max limit is specified.

To retrieve only a limited number of results you should specify that number, like this:

// Get only the first 2 entries
$entries = $xml->GetItems( 2 );
public function GetAll()

This function selects everything from the $_doc variable and returns the result as an associated array.

class AbsRssWriter20

This class can be used to create an RSS 2.0 xml feed document.

Protected Properties

/**
* Holds the feed's content
* @type string
*/
protected static $_doc;

Public Methods

public function StartDocument( $xmlStylesheetFile = '' ){}
public function AddNamespaces( $xmlns = array() ){}
public function AddChannelTags( array $channelTags ){}
public function AddItems( $itemTags = array() ){}
public function EndDocument(){}
public function Display(){}
public function GetDocument(){}
public function SaveDocument( $dirPath, $fileName ){}
public function StartDocument( $xmlStylesheetFile = ” )

Starts the xml document. The optional argument is the path to the xml stylesheet file.

require( "../classes/class.AbsRssWriter20.php" );
 
$xml = new AbsRssWriter20();
 
// START DOCUMENT
$xml->StartDocument('xsl_stylesheet.xsl');

Will result to:

<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type="text/xsl" href="xsl_stylesheet.xsl"?><rss version="2.0"
public function AddNamespaces( $xmlns = array() )

This function adds the provided namespaces to the document. These are optional.

// ADD NAMESPACES
$ns = array(
   'slash' => 'http://purl.org/rss/1.0/modules/slash/'
  ,'content' => 'http://purl.org/rss/1.0/modules/content/'
  ,'wfw' => 'http://wellformedweb.org/CommentAPI/'
  ,'dc' => 'http://purl.org/dc/elements/1.1/'
);
$xml->AddNamespaces($ns);

Will result to:

xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
>
public function AddChannelTags( array $channelTags )

This function adds the provided channel tags.

// ADD CHANNEL TAGS
$channelTags = array(
   'title' => 'Latest entries on: Blog Name Here'
  ,'link' => 'http://blog-url-here/'
  ,'description' => "Coding is fun!"
  ,'pubDate' => 'Mon, 10 Apr 2009 22:00:40'
  ,'generator' => 'Blog Name Here'
  ,'language' => 'en-us'
  ,'dc:publisher' => 'Costin Trifan'
  ,'copyright' => 'Copyright (c) 2008 Costin Trifan. All rights reserved. blah blah..
);
$xml->AddChannelTags($channelTags);

Will result to:

<channel>
<title>Latest entries on: Blog Name Here</title>
<link>http://blog-url-here/</link>
<description>Coding is fun!</description>
<pubDate>Mon, 10 Apr 2009 22:00:40</pubDate>
<generator>Blog Name Here</generator>
<language>en-us</language>
<dc:publisher>Costin Trifan</dc:publisher>
<copyright>Copyright (c) 2008 Costin Trifan. All rights reserved. blah blah..</copyright>
public function AddItems( $itemTags = array() )

This function adds the provided item tags to the document. The $itemTags parameter should be provided as a bidimensional array.

// ADD ENTRIES // A bidimensional array is required!
$itemTags = array(
  array(
     'title' => 'Post 1'
    ,'link' => 'http://blog-url-here/post.php?pid=11'
    ,'description' => "<![CDATA[Posts's short description goes here]]>"
    ,'slash:comments' => 40
    ,'comments' => 'http://blog-name-here/post.php?pid=11#comments'
    ,'pubDate' => 'Sun, 12 Apr 2009 02:01:52 GMT'
    ,'category' => 'PHP'
  ),
  array(
     'title' => 'Post Title 2'
    ,'link' => 'http://blog-url-here/post.php?pid=10'
    ,'description' => "<![CDATA[Posts's short description goes here]]>"
    ,'slash:comments' => 120
    ,'comments' => 'http://blog-name-here/post.php?pid=10#comments'
    ,'pubDate' => 'Sun, 11 Apr 2009 02:01:52 GMT'
    ,'category' => 'WEB'
  )
);
$xml->AddItems($itemTags);

Will result to:

<item>
  <title>Post 1</title>
  <link>http://blog-url-here/post.php?pid=11</link>
  <description><![CDATA[Posts's short description goes here]]></description>
  <slash:comments>40</slash:comments>
  <comments>http://blog-name-here/post.php?pid=11#comments</comments>
  <pubDate>Sun, 12 Apr 2009 02:01:52 GMT</pubDate>
  <category>PHP</category>
</item>
 
<item>
  <title>Post 2</title>
  <link>http://blog-url-here/post.php?pid=10</link>
  <description><![CDATA[Posts's short description goes here]]></description>
  <slash:comments>120</slash:comments>
  <comments>http://blog-name-here/post.php?pid=10#comments</comments>
  <pubDate>Sun, 11 Apr 2009 02:01:52 GMT</pubDate>
  <category>WEB</category>
</item>

Note that you should escape special characters before adding them into the document.

public function EndDocument()

This function adds the closing document’s tags.

// END DOCUMENT
$xml->EndDocument();

Will result to:

</channel></rss>
public function Display()

This function will display the generated xml feed.

// DISPLAY CONTENT
$xml->Display();
public function GetDocument()

This function returns the content of the document.

// GET DOCUMENT
$content = $xml->GetDocument();

Now, the $content variable will contain the generated xml document;

public function SaveDocument( $dirPath, $fileName )

This function will save the generated xml feed into the specified file($fileName) in the directory($dirPath).

// SAVE THE FEED'S CONTENT INTO AN XML FILE
try {
  if ($xml->SaveDocument(getcwd(),'rss_2.xml')) {
    exit('The feed has been successfully created!');
  }
}
catch(Exception $ex) { exit($ex->getMessage()); }

These classes have basically saved my a$$ many times since I created them. I only hope they’ll be as useful to you as they are to me!

Download AbsXmlRss archive

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>