By Zarko Gajic
XML RSS Feed in a TreeView
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> ...
First, create a record structure (and a pointer to it) to hold RSS item data:
type PRSSFeedData = ^TRSSFeedData; TRSSFeedData = record URL : string; Description : string; Date : string; end;
The PopulateTree procedure locates the first "item" node in the RSS and creates a tree node for each item: procedure TRSSTreeForm.PopulateTree; 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://0.tqn.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;
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; var cnt : integer; begin for cnt := 0 to TreeView1.Items.Count - 1 do Dispose(TreeView1.Items[cnt].Data) ; TreeView1.Items.Clear; end;
procedure TRSSTreeForm.TreeView1Change(Sender: TObject; Node: TTreeNode) ; 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;
That's it. Download the code .. and learn from it - by modifying of course :)联系客服