打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

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

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

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

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

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
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
xml基础学习笔记03
如何打造自己的RSS feed
Newsalloy是迄今为止我见过最完美的一个在线RSS阅读器了,看了Newsalloy,你就会感觉以前使用的其他RSS阅读器简直不能望其项背。
刘韧Blog RSS的繁荣基于Tag应用的普及
IOS 一些好的框架和 技术大牛的博客
图解ECSHOP商城优化feed.php删除?from=rss方法
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服