uha.. jeg fik lige kodet en god del her!
dette kræver sysutils og classes i uses:
type
tkeys = record
name, val: string;
end;
txmlnode = class;
txmlparser = class;
txmlnode = class
owner: txmlparser;
parent: txmlnode;
name, text: string;
args: array of tkeys;
nodes: tlist;
function nodecount: integer;
function getnode(index: integer): txmlnode; overload;
function getnode(name: string): txmlnode; overload;
destructor remove;
constructor parse(var str: AnsiString);
end;
txmlparser = class
nodes: tlist;
constructor create(filename: string);
destructor destroy;
end;
trssitem = record
title, subject,link,description,Author, dato: string;
end;
trsschannel = record
title, link,description,language,publisher,subject, dato: string;
end;
trssfile = class
info: trsschannel;
items: array of trssitem;
procedure loadxml(filename: string);
destructor destroy;
end;
procedure trssfile.loadxml(filename: string);
var xml: txmlparser;
i: integer;
begin
xml := txmlparser.create(filename);
with txmlnode(xml.nodes.items[0]) do
begin
with getnode('channel') do
begin
info.title := getnode('title').text;
info.link := getnode('link').text;
info.description := getnode('description').text;
info.language := getnode('dc:language').text;
info.publisher := getnode('dc:publisher').text;
info.subject := getnode('dc:subject').text;
info.dato := getnode('dc:date').text;
end;
for i := 2 to nodecount-1 do
begin
setlength(items,high(items)+2);
with getnode(i) do
begin
items[high(items)].title := getnode('title').text;
items[high(items)].subject := getnode('dc:subject').text;
items[high(items)].author := getnode('dc:creator').text;
items[high(items)].link := getnode('link').text;
items[high(items)].dato := getnode('dc:date').text;
items[high(items)].description := getnode('description').text;
end;
end;
end;
xml.destroy;
end;
destructor trssfile.destroy;
begin
setlength(items,0);
items := nil;
inherited destroy;
end;
function txmlnode.nodecount: integer;
begin
result := nodes.count;
end;
function txmlnode.getnode(name: string): txmlnode;
var i: integer;
begin
result := nil;
for i := 0 to nodes.Count-1 do
if txmlnode(nodes.Items[i ]).name = name then
begin
result := txmlnode(nodes.Items[i ]);
exit;
end;
end;
function txmlnode.getnode(index: integer): txmlnode;
begin
result := txmlnode(nodes.items[index]);
end;
constructor txmlnode.parse(var str: AnsiString);
var t: AnsiString;
n: txmlnode;
begin
inherited create;
nodes := tlist.Create;
delete(str,1,1);
name := copy(str,1,pos('>', str)-1);
if pos(' ',name) > 0 then
name := copy(str,1,pos(' ',str)-1);
delete(str,1,length(name));
t := (copy(str,1,pos('>',str)-1));
while length(t) > 1 do
begin
setlength(args, high(args)+2);
args[high(args)].name := copy(t, 1, pos('="',t)-1);
delete(t,1,pos('="',t)+1);
args[high(args)].val := copy(t,1,pos('"',t)-1);
delete(t, 1, pos('"', t));
end;
delete(str,1,pos('>',str));
t := copy(str,1,pos('</' + name + '>',str)-1);
delete(str,1,pos('</' + name + '>',str)+length('</' + name + '>')-1);
if pos('<',t) = 0 then
begin
text := t;
exit;
end;
while pos('<',t) > 0 do
begin
n := txmlnode.parse(t);
nodes.Add(n);
end;
end;
destructor txmlnode.remove;
var i: integer;
begin
for i := nodes.count-10 downto 0 do
txmlnode(nodes.Items[i ]).remove;
if parent = nil then
begin
owner.nodes.Remove(self);
end
else
begin
parent.nodes.Remove(self);
end;
nodes.Destroy;
inherited destroy;
end;
constructor txmlparser.create(filename: string);
var f: tstringlist;
str: AnsiString;
n: txmlnode;
begin
inherited create;
nodes := tlist.Create;
f := tstringlist.Create;
f.LoadFromFile(filename);
str := stringreplace(f.text, #13#10,'',[rfreplaceall]);
f.Destroy;
if pos('<?xml',str) = 1 then
delete(str,1,pos('?>',str)+1);
while pos('<',str) > 0 do
begin
n := txmlnode.parse(str);
nodes.Add(n);
end;
end;
destructor txmlparser.destroy;
var i: integer;
begin
for i := nodes.count-10 downto 0 do
txmlnode(nodes.Items[i ]).remove;
nodes.Destroy;
inherited destroy;
end;
du benytter blot loadxml proceduren på en trssfile du creater. Så henter den alle informationerne og ligger data om rss feeden i info recorden og alle items ligger i items array'et
Resten tror jeg giver sig selv ret meget..
Jeppe Johansen
Jeppesoftware@hotmail.com
[Redigeret d. 17/01-06 23:25:38 af Jeppe]