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 <channel> or <item> block
if ($name = = "ITEM")
{
$this->flag = 1,
}
else if ($name = = "CHANNEL")
{
$this->flag = 2,
}
}
Closing Tag Handler
function Elementend ($parser, $name)
{
$this->currenttag = "",
Set flag if exiting <channel> or <item> block
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->currenttag = =
"LINK" | | $this->currenttag = "DESCRIPTION")
{
Add data to $channels [] or $items [] array
if ($this->flag = = 1)
{
$this->items[$this->count][strtolower ($this->currenttag)]. = $data,
}
else if ($this->flag = 2)
{
$this->channel[strtolower ($this->currenttag)]. = $data,
}
}
}
Return a associative array containing channel information
(The $channel [] array)
function Getchannelinfo ()
{
Return $this->channel,
}
Return a associative array of arrays containing item
Information
(The $items [] array)
function GetItems ()
{
Return $this->items,
}
}
?>
If you are familiar with the PHP class, then it is quite easy to understand this piece of code. If you do not understand, then skip to the link section at the end of the article to see a good article about the working principle of the class. Then go back and read the code above.
Before using this class, I'll take a few minutes to point out one line of code-that is, the line above the Xml_set_object () function call.
The question now is how to use this class to actually generate a Web page with multiple content sources.
?
Include ("Class.") Rdfparser.php "),
How many items to display in each channel
$maxItems = 5,
?>
<basefont face= "Verdana" >
<body>
<table width= "100%" border= "0" cellspacing= "5" cellpadding= "5" > <tr>
<!--the--> of the cell
<TD Valign=top align=left>
<font size= "-1" >
?
Get and Parse Freshmeat.net channel
$f = new Rdfparser (),
$f->setresource ("HTTP://WWW.FRESHMEAT.NET/BACKEND/FM-RELEASES.RDF"),
$f->parseresource (),
$f _channel = $f->getchannelinfo (),
$f _items = $f->getitems (),
Now format and print it ...
?>
The latest from <a href=<? echo $f _channel["link"], >><? Echo
$f _channel["title"],?></a> <br> <ul> Iterate through items array
For ($x =0, $x < $maxItems, $x + +) {
if (Is_array ($f _items[$x]))
{
Print data
$item = $f _items[$x],
echo "<li><a href=". $item ["link"]. ">".
$item ["title"]. "</a>",
}
}
?>
</ul>
</font>
</td>
<!--second cell-->
<TD Align=center width=50%>
<i>primary page Content here</i>
</td>
<!--third cell-->
<TD Valign=top align=left>
<font size= "-1" >
?
Get and Parse slashdot.org channel
$s = new Rdfparser (),
$s->setresource ("HTTP://SLASHDOT.ORG/SLASHDOT.RDF"),
$s->parseresource (),
$s _channel = $s->getchannelinfo (),
$s _items = $s->getitems (),
Now format and print it ...
?>
The latest from <a href=<? echo $s _channel["link"], >><? Echo
$s _channel["title"],?></a> <br> <ul> Iterate through items array
For ($x =0, $x < $maxItems, $x + +) {
if (Is_array ($s _items[$x]))
{
Print data
$item = $s _items[$x],
echo "<li><a href=". $item ["link"]. ">".
$item ["title"]. "</a>",
}
}
?>
</ul>
</font>
</td>
</tr>
</table>
</body>
This piece of code is fairly straightforward. Once you use the "new" keyword to generate an instance of a class,
$f = new Rdfparser (),
You can then use the class method to set the location of the RDF file to parse.
$f->setresource ("HTTP://WWW.FRESHMEAT.NET/BACKEND/FM-RELEASES.RDF"),
$f->parseresource (),
And get the $channel and $items arrays for later processing.
?
$f _channel = $f->getchannelinfo (),
$f _items = $f->getitems (),
?>
The latest from <a href=<? echo $f _channel["link"], >><? Echo
$f _channel["title"],?></a> <br> <ul> Iterate through items array
For ($x =0, $x < $maxItems, $x + +) {
if (Is_array ($f _items[$x]))
{
Print data
$item = $f _items[$x],