unit RssReader;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, OleCtrls, SHDocVw, ExtCtrls, xmldom, XMLIntf,
msxmldom, XMLDoc, DB, ADODB, ComCtrls, ImgList, ActiveX;
type
TForm1 = class(TForm)
Panel1: TPanel;
WebBrowser1: TWebBrowser;
XMLDocument1: TXMLDocument;
ADOTable1: TADOTable;
DataSource1: TDataSource;
ListView1: TListView;
ImageList1: TImageList;
TreeView1: TTreeView;
XMLDocument2: TXMLDocument;
Memo1: TMemo;
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure ListView1Click(Sender: TObject);
procedure ListView1DblClick(Sender: TObject);
procedure ListView1DrawItem(Sender: TCustomListView;
Item: TListItem;
Rect: TRect;
State: TOwnerDrawState);
proceduredo
mToTree (XmlNode: IXMLNode;
TreeNode: TTreeNode);
procedure getData(XMLfile:string);
procedure TreeView1DblClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
// public paraments
count:integer;
flag : Integer;
Ptitle,Plink:string;
cv_name,cv_link:array[0..10] of string;
implementation
{$R *.dfm}
procedure TForm1.DomToTree(XmlNode: IXMLNode;
TreeNode: TTreeNode);
var
I: Integer;
NewTreeNode: TTreeNode;
NodeText: string;
AttrNode: IXMLNode;
begin
if not (XmlNode.NodeType = ntElement) then
Exit;
NodeText := XmlNode.NodeName;
if XmlNode.IsTextElement then
NodeText := NodeText + ' = ' + XmlNode.NodeValue;
if xmlnode.HasAttribute('name')=true then
nodetext := xmlnode.Attributes['name'];
// if nodename equals 'link',end it. then
write to array
if xmlnode.NodeName <> 'link' then
NewTreeNode := TreeView1.Items.AddChild(TreeNode, NodeText)
else
begin
cv_name[strtoint(xmlnode.NextSibling.Text)]:=xmlnode.ParentNode.Attributes['name'];
cv_link[strtoint(xmlnode.NextSibling.Text)]:=xmlnode.Text;
end;
// add each child node
if XmlNode.HasChildNodes then
for i:= 0 to xmlNode.ChildNodes.Count - 1do
if xmlnode.ChildNodes.NodeName <> 'order' then
do
mToTree (xmlNode.ChildNodes.Nodes, NewTreeNode);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
count:=0;
flag:=0;
ADOTable1.Active :=True;
memo1.Clear;
do
mToTree (XMLDocument2.DocumentElement, nil);
end;
procedure TForm1.getData(XMLfile: string);
var
channel,item,first:IXMLNode;
title,link,description,pubDate,category:string;
newItem:TListItem;
i,flag:integer;
begin
flag:=0;
xmldocument1.Active :=False;
xmldocument1.FileName:='';
xmldocument1.FileName:=ExtractFilePath(Application.ExeName) + XMLfile;
xmldocument1.Active :=True;
try
newItem:=nil;
channel:=xmldocument1.DocumentElement.ChildNodes[0];
item:=channel.ChildNodes['item'];
first:=item;
while item.NextSibling <> nildo
begin
inc(count);
item:=item.NextSibling;
end;
item:=first;
for i:=0 to count-1do
begin
title:=item.ChildNodes['title'].text;
link:=item.ChildNodes['link'].Text;
description:=item.ChildNodes['description'].Text;
pubDate:=item.ChildNodes['pubDate'].Text;
category:=item.ChildNodes['category'].Text;
adotable1.First;
flag :=0;
while not adotable1.Eof do
begin
if adotable1.FieldByName('title').asstring = title then
begin
flag:=1;
break;
end;
adotable1.Next;
end;
// write date to database
if flag = 0 then
begin
adotable1.Append;
adotable1.FieldByName('title').Value:=title;
adotable1.FieldByName('link').Value:=link;
adotable1.FieldByName('description').Value:=description;
adotable1.FieldByName('pubDate').Value:=pubDate;
adotable1.FieldByName('category').value:=category;
adotable1.Post;
end;
// display the xml data to Listview
newItem:=listview1.Items.Add;
newItem.Caption:=title;
newItem.ImageIndex:=1;
if item.NextSibling<>nil then
item:=item.NextSibling
else
Break;
end;
finally
xmldocument1.Active :=False;
xmldocument1.FileName:='';
end;
end;
procedure TForm1.ListView1Click(Sender: TObject);
var locate:integer;
begin
if listview1.Selected <> nil then
begin
memo1.Clear;
locate:=listview1.Selected.Index;
adotable1.Locate('title',listview1.Items[locate].Caption,[loCaseInsensitive]);
memo1.Lines.Add(adotable1.Fields[0].AsString);
memo1.Lines.Add(adotable1.Fields[2].AsString);
Plink:=adotable1.Fields[1].AsString;
memo1.Lines.Add(Plink);
end;
end;
procedure TForm1.ListView1DblClick(Sender: TObject);
var
text:string;
index:integer;
insertItem:TlistItem;
begin
webbrowser1.Navigate(Plink);
if ListView1.Selected<>nil then
//此处已更改
begin
listview1.Selected.cut:=true;
text:=listview1.Selected.Caption;
index:=listview1.Selected.Index;
listview1.Selected.Delete;
insertitem:=listview1.items.insert (index);
insertitem.Caption:=text;
end;
end;
procedure TForm1.ListView1DrawItem(Sender: TCustomListView;
Item: TListItem;
Rect: TRect;
State: TOwnerDrawState);
begin
listview1.Canvas.Font.Color:=clblue;
end;
procedure TForm1.TreeView1DblClick(Sender: TObject);
var
i:integer;
begin
for i:=0 to 9do
begin
if cv_name = treeview1.Selected.Text then
begin
//showmessage(cv_link);
getdata(cv_link);
end;
end;
end;
procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
ADOTable1.Close;
end;
initialization
CoInitialize(nil);
finalization
CoUninitialize;
end.
这么贴上来了