Read more: SOURCE CODE: Displaying XML (RSS Feed) Data in a TreeView Delphi control

Friday, May 6, 2011

Displaying XML (RSS Feed) Data in a TreeView Delphi control

RSS stands for really simple syndication. RSS is a family of web feeds used to represent digital content in the XML format. RSS feeds typically list items, such as articles or blogs posts.
The structure of the RSS feed (basically any XML document) is similar to the structure of a tree view.

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:

 PRSSFeedData = ^TRSSFeedData;
 TRSSFeedData = record
 URL : string;
 Description : string;
 Date : string;
The PopulateTree procedure locates the first "item" node in the RSS and creates a tree node for each item:
 procedure TRSSTreeForm.PopulateTree;
 nd : IXMLNode;
 procedure ProcessItem() ;
 rssFeedData : PRSSFeedData;
 tn : TTreeNode;
 title : string;
 New(rssFeedData) ;
 title := nd.ChildNodes.FindNode('title').Text;
 with nd.ChildNodes do
 rssFeedData.URL := FindNode('link').Text;
 rssFeedData.Description := FindNode('description').Text;
 rssFeedData.Date := FindNode('dc:date').Text;
 tn := TreeView1.Items.AddChild(TreeView1.Items.GetFirstNode, title) ;
 tn.Data := rssFeedData;
 end; (*NESTED ProcessItem*)
 //ADPHealines.xml is RSS download
 XMLDocument1.FileName := 'ADPHealines.xml';
 XMLDocument1.Active := true;
 nd := XMLDocument1.DocumentElement.ChildNodes.First;
 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,'') ;
 //loop through "items"
 while nd <> nil do
 ProcessItem() ;
 nd := nd.NextSibling;
 //expand tree
 TreeView1.Items.GetFirstNode.Expand(true) ;
 //"close" RSS XML file
 XMLDocument1.Active := false;
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.
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;
 cnt : integer;
 for cnt := 0 to TreeView1.Items.Count - 1 do
 Dispose(TreeView1.Items[cnt].Data) ;
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) ;
 rssFeedData : PRSSFeedData;
 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]) ;

1 comment:

html5 media player said...

Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write ups thanks once again.
html5 music player