Return to class (Back to Class)
Since you have such a big power, why should you limit your own RDF source? As I have said earlier, most of the main sites often make snapshots for what they provide. In fact, insert all these different sources into your site is quite simple. Let us see how it is done.
First, we modularize the code in the previous example. In this way, you don't have to overwrite the same code over again over again. The simplified method is to pack it, and then this class contains this class into my PHP script.
The class code is as follows:
Class RdfParser
{
//
// Variables
//
// set up local variables for this class
Var $ currentTAG = ""
VAR $ FLAG = ""
Var $ count = 0;
// this is an associative array of channel data with keys
("Title", "LINK", "Description")
Var $ CHANNEL = array ();
// this is an array of arrays, with Each Array ELEMENT
Representing an
// Each Outer Array Element is itself an associative array
// with keys ("Title", "LINK", "Description")
Var $ items = array ();
//
//Methods
//
// set the name of the rdf file to parse
// this is usually a local file
// You May Set It to a Remote File if your php build supports
URL FOPEN ()
Function setResource ($ file)
{
$ this-> file = $ file;
}
// Parse The Rdf File Set with setResource ()
// this populates the $ channel and $ Items arrays
Function Parsresource ()
{
// Create Parser
$ this-> xp = XML_PARSER_CREATE ();
// set Object Reference
XML_SET_OBJECT ($ this-> XP, $ this);
// set handlers and Parser Options
XML_SET_ELEMENT_HANDLER ($ this-> XP, "ElementBegin",
"Elementend");
XML_SET_CHARACTER_DATA_HANDLER ($ this-> XP,
"characterdata");
XML_Parser_Set_Option ($ this-> XP,
XML_OPTION_CASE_FOLDING, TRUE);
XML_PARSER_SET_OPTION ($ this-> XP, XML_OPTION_SKIP_WHITE,
True);
// read XML File
IF (! ($ fp = fopen ($ this-> file, "r")))))
Die ("Could Not Read $ THIS-> File");
}
// Parse Data
While ($ XML = FREAD ($ FP, 4096))
{
IF (! XML_PARSE ($ this-> XP, $ XML, Feof ($ fp)))))
{
DIE ("XML Parser Error:".
XML_ERROR_STRING (XML_GET_ERROR_CODE ($ THIS-> XP))));
}
}
// Destroy Parser
XML_Parser_Free ($ this-> XP);
}
// Opening Tag Handler
Function ElementBegin ($ PARSER, $ Name, $ Attributes)
{
$ this-> CurrentTag = $ name;
// set flag if entering
IF ($ Name == "Item")
{
$ this-> Flag = 1;
}
Else IF ($ name == "channel")
{
$ this-> Flag = 2;
}
}
// Closing Tag Handler
Function Elementend ($ Parser, $ Name)
{
$ this-> currittag = ""
// set flag if exitation
IF ($ Name == "Item")
{
$ this-> count ;
$ this-> Flag = 0;
}
Else IF ($ name == "channel")
{
$ this-> Flag = 0;
}
}
// Character Data Handler
Function CharacterData ($ Parser, $ DATA)
{
$ DATA = Trim (HTMLSpecialchars ($ data));
IF ($ this-> currenttag == "title" || $ this-> currittag ==
"LINK" || $ this-> currenttag == "description")
{
// add data to $ channels [] or $ items [] array
IF ($ this-> flag == 1)
{
$ this-> items [$ this-> count] [STRTOLOWER ($ this-> currittag)]. = $ data;
}
ELSE IF ($ this-> flag == 2)
{
$ this-> Channel [STRTOLOWER ($ this-> currittag)]. = $ data;
}
}
}
// Return an associative array containing channel information
// (The $ channel [] array)
Function getChannelInfo ()
{
Return $ this-> channel;
}
// Return an associative array of arrays containing itemformation
// (The $ item [] array)
Function GetItems ()
{
Return $ this-> item;
}
}
?>
If you are more familiar with the PHP class, then understand this code is quite easy. If you don't know much, please jump directly to the link to the end of the article, see a good article about the principle of work. Then come back to continue reading the code above.
Before using this class, I have to take a few minutes to point out a line of code - the line on the XML_SET_Object () function is called.
The current problem is how to use this class to actually generate a web page with multiple content sources.
Include ("Class.rdfParser.php");
// How much items to display in Each Channel
$ MAXITEMS = 5;
?>
// Get and Parse FreshMeat.Net Channel $ f = new rdfparser (); $ f-> setResource ("http://www.freshmeat.net/backend/fm-releases.rdf"); $ f-> parsresource (); $ F_CHANNEL = $ f-> getChannelInfo (); $ f_items = $ f-> getItems (); // Now format and print it ... ?> The Latest from > Echo $ F_CHANNEL ["Title"];?>
For ($ x = 0; $ x <$ maxItems; $ x ) { IF (Is_Array ($ F_ITEMS [$ x]))) { // Print Data $ item = $ f_items [$ x]; Echo " |