分享

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

 quasiceo 2013-10-26

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

Displaying XML (RSS Feed) Data in a TTreeView. Storing Data in Tree Items.

By

XML RSS Feed in a TreeView

XML RSS Feed in a TreeView

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.

 <rss version="2.0">
 <channel>
 <title>About Delphi Programming</title>
 <link>http://delphi./</link>
 <description>Delphi Programming</description>
 <item>
 <title>Programmatically Get Memory Status using Delphi</title>
 <link>http://delphi.
/b/a/257760.htm</link>
 <description>...</description>
 <guid isPermaLink="true">http://delphi.
/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

  1. Get the RSS Feed (XML) from the Internet,
  2. Parse the XML and fill the TreeView,
  3. Each item in the RSS creates one tree node in the tree view,
  4. 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:

 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./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;
 

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) ;
 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 :)
Source Code

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多