Finishing an RSS Reader - New code
(Page 2 of 4 )
Now that we've done the set up, let's get on with the code. First, let's add new code to the OnCreate event. Go to the object inspectors' event tab and double click on the OnCreate event. Add the following code before the last "end;" of the procedure :
Var
rec:TSearchRec;
....
if not DirectoryExists('MyFeeds') then createdir('MyFeeds');
// find all files matching *.xml in the current dir
if FindFirst('MyFeeds*.xml', faAnyFile, rec) = 0 then
begin
with tv.Items.AddFirst(nil, 'MyFeeds') do
begin
Selected:=true;
end;
repeat
with tv.Items.AddChildFirst( tv.Selected, rec.Name) do
begin
MakeVisible;
end;
until FindNext(rec) <> 0;
// Must free up resources used by these successful finds
FindClose(rec);
end
else begin
with tv.Items.AddFirst( nil, 'No Files Found' ) do
begin
Selected := true;
end;
end;
This piece of code searches for all the files with a ".xml" extension and then adds them to the treeview.
Next, we need to open up the XML file when the user clicks on a filename from the XML treeview, so select the treeview component and go to the object inspectors' events tab. Double click on the DblClick event and add the following code:
procedure TForm1.tvxmlDblClick(Sender: TObject);
var
StartItemNode : IXMLNode;
ANode : IXMLNode;
STitle, sDesc, sLink : WideString;
begin
LV.Clear;
if (tvxml.Selected.Level <> 0) then begin
fn:=tv.Selected.Text;
form1.Caption:='';
form1.Caption:='RSS XML File Reader - '+fn;
//if not DirectoryExists('MyFeeds') then CreateDir('MyFeeds');
//points to local XML file in "original" code
XMLDoc.FileName :='MyFeeds'+fn;
XMLDoc.Active:=True;
StartItemNode := XMLDoc.DocumentElement.ChildNodes.First.ChildNodes.FindNode('item') ;
if StartItemNode = nil then begin
showmessage('Error');
exit;
end;
ANode := StartItemNode;
repeat
STitle := ANode.ChildNodes['title'].Text;
sLink := ANode.ChildNodes['link'].Text;
sDesc := ANode.ChildNodes['description'].Text;
//add to list view
with LV.Items.Add do
begin
Caption := STitle;
SubItems.Add(sLink) ;
SubItems.Add(sDesc)
end;
ANode := ANode.NextSibling;
until ANode = nil;
end;
end;
As you've probably worked out by now, the code parses the contents of the XML file onto the listview component, based on the Title, Link and Description tags. Have a look at the parser in action below:

Next: Getting files from a website >>
More Delphi-Kylix Articles
More By Jacques Noah