The structure of the RSS feed (basically any XML document) is similar to the structure of a tree view.
<rss version="2.0">
<channel>
<title>About Delphi Programming</title>
<link>http://delphi.about.com/</link>
<description>Delphi Programming</description>
<item>
<title>Programmatically Get Memory Status using Delphi</title>
<link>http://delphi.about.com
/b/a/257760.htm</link>
<description>...</description>
<guid isPermaLink="true">http://delphi.about.com
/b/a/257760.htm</guid>
<dc:subject/>
<dc:date>2007-03-24T08:03:51Z</dc:date>
</item>
...
Download XML (RSS Feed) from the Internet
To download the RSS feed for this site, point your browser to the Stay up-to-date with Delphi programming. To programmatically download this XML file, you can use the code provided in the Download a file from the Internet with progress indicator article.XML (RSS Feed) to TreeView
To create a simple "RSS Feed Tree-View" reader, start by dropping a TTreeView control on a Delphi form. You'll aslo need the TXMLDocument and a few labels.Populate RSS Feed Tree
- Get the RSS Feed (XML) from the Internet,
- Parse the XML and fill the TreeView,
- Each item in the RSS creates one tree node in the tree view,
- The Data property of a tree node holds the RSS item data: link, description, date, etc.
First, create a record structure (and a pointer to it) to hold RSS item data:
typeThe PopulateTree procedure locates the first "item" node in the RSS and creates a tree node for each item:
PRSSFeedData = ^TRSSFeedData;
TRSSFeedData = record
URL : string;
Description : string;
Date : string;
end;
procedure TRSSTreeForm.PopulateTree;Note: for each "item" node in the RSS, the nested procedure ProcessItem is used to create a new tree node and attach it the rssFeedData data.
var
nd : IXMLNode;
procedure ProcessItem() ;
var
rssFeedData : PRSSFeedData;
tn : TTreeNode;
title : string;
begin
New(rssFeedData) ;
title := nd.ChildNodes.FindNode('title').Text;
with nd.ChildNodes do
begin
rssFeedData.URL := FindNode('link').Text;
rssFeedData.Description := FindNode('description').Text;
rssFeedData.Date := FindNode('dc:date').Text;
end;
tn := TreeView1.Items.AddChild(TreeView1.Items.GetFirstNode, title) ;
tn.Data := rssFeedData;
end; (*NESTED ProcessItem*)
begin
ClearTree;
//ADPHealines.xml is RSS download
XMLDocument1.FileName := 'ADPHealines.xml';
XMLDocument1.Active := true;
//"chanel"
nd := XMLDocument1.DocumentElement.ChildNodes.First;
//title
nd := nd.ChildNodes.First;
Caption := nd.Text + ' RSS Feed';
while nd.NodeName <> 'item' do nd := nd.NextSibling;
//add top tree node
TreeView1.Items.AddChild(nil,'http://z.about.com/6/g/delphi/b/index.xml') ;
//loop through "items"
while nd <> nil do
begin
ProcessItem() ;
nd := nd.NextSibling;
end;
//expand tree
TreeView1.Items.GetFirstNode.Expand(true) ;
//"close" RSS XML file
XMLDocument1.Active := false;
end;
In order to prevent memory leaks, you need to make sure memory contained in the Data property of a tree item is fred. This is done in the "ClearTree" procedure:
procedure TRSSTreeForm.ClearTree;
var
cnt : integer;
begin
for cnt := 0 to TreeView1.Items.Count - 1 do
Dispose(TreeView1.Items[cnt].Data) ;
TreeView1.Items.Clear;
end;
Displaying RSS Tree Data
To display the information for the rss "item" contained in the tree view item, use the TreeView OnChange event:procedure TRSSTreeForm.TreeView1Change(Sender: TObject; Node: TTreeNode) ;That's it. Download the code .. and learn from it - by modifying of course :)
var
rssFeedData : PRSSFeedData;
begin
if TreeView1.Selected = nil then Exit;
if TreeView1.Selected = TreeView1.Items.GetFirstNode then Exit;
//all other tree items except the "first" one hold rss data ...
rssFeedData := PRSSFeedData(TreeView1.Selected.Data) ;
lblTitle.Caption := TreeView1.Selected.Text;
lblLink.Caption := Format('URL: %s',[rssFeedData.URL]) ;
lblDescription.Caption := Format('Description: %s',[rssFeedData.Description]) ;
lblDate.Caption := Format('Date: %s',[rssFeedData.Date]) ;
end;


