Using CDATA in Feed Output

September 10th, 2011
by Daniel Soutter

Php FeedWriter supports output of CDATA for a number of data types used in each feed format.  The default data types are set in the FeedConstruct class for each format, which help to control the output of the feed XML.  When CDATA is enabled, elements that use a data type that supports CDATA will be written using the writeCData() method of the XMLWriter class, instead of the writeRaw() or text() methods.

The data types that use CDATA when it is enabled are:

  • string
  • email
  • html

It is also possible to override the default data type set in a feed format prior to outputting the feed if you would like additional elements to be outputted using CDATA.  To do this, you first need to enable CDATA when instantiating a feed object from the FeedWriter class using the “useCDATA” (boolean) parameter of the class constructor (5th parameter).

$feed = new FeedWriter(
		'Php FeedWriter Sample Feed', 	//Feed Title
		'Generate an Atom 1.0, RSS 2.0 and other feeds from website or database content', //Feed Description
		'http://phpfeedwriter.webmasterhub.net/', //Feed Link
		6, //indent
		true, //Use CDATA
		null, //encoding
		true //enable validation
		);

To override a default data type in order to allow it to be outputted using CDATA, you can modify the data type of a specific element in the feed schema for a particular format to one of the three data types listed above.  The example below will modify the “itemLink” construct (link element of feed items), so that it is written using CDATA instead of the default.

If CDATA has been enabled, the next step is to determine and set the output format for the feed.  The data type of the itemLink construct can then be changed to one which uses CDATA.

//Get the format to display in,
//set to RSS 2.0 as default or if format is not found
$format = RSS_2_0;
if(isset($_GET['format']) && $_GET['format'] != null)
{
	foreach($feed->getFeedFormats() as $curFormat)
	{
		if($curFormat[0] == $_GET['format'])
		$format = $curFormat[1];
	}
}

Now that we have the desired output format stored in the $format variable, this can be used to obtain an instance of the FeedConstruct class for this format allowing specific elements to be modified at run-time.  The construct for the link element of a feed item is “itemLink”.

//Set the feed item link output type to text for the feed format
$feed->set_feedConstruct($format);
$feed->feed_construct->construct['itemLink']['type'] = 'string';

Now when the feed is outputted, the item link element will be of type ‘string’ instead of the default (uri).  This will result in item link elements being written by the XMLWriter as CDATA ( writeCDATA() ) instead of writeRaw().

 

  • For a full list of the supported data types used for elements in each feed schema, see Output Data Types.
  • For a list of the “constructs” such as “itemLink” that make up each feed format, please see Predefined Feed Constructs.

Tags:
Posted in add_item(), FeedWriter Class, __construct() | Comments (0)

  • Php FeedWriter (c) Copyright 2011 Daniel Soutter. Php FeedWriter and the information on this site has been designed to benefit website developers, owners and users, but may contain errors and is provided as is, with no warranty. Terms.