对于有n多记录的xml,可以填充到stringgrid中
其中 vkeynode 为 xml中 重复节点
function CommonAnalyzeXml(vxml,vkeynode: string;var vgrid: TStringGrid): Boolean;
var
lxml: TNativeXml;
i,j,k: Integer;
llist: TsdNodeList;
lnode: TXmlNode;
lstr: string;
ls,lnamels: TStringList;
begin
Result := False;
vxml := StringReplace(vxml,'GB2312','UTF-8',[rfReplaceAll]);
lxml := TNativeXml.Create(nil);
lxml.ReadFromString(vxml);
lnode := lxml.Root;
if lnode <> nil then
begin
lnamels := TStringList.Create;
GetXmlAllValues(lnamels,lnode,True);
for i := 1 to vgrid.rowcount do
for j := 1 to vgrid.colcount do
vgrid.cells[j - 1, i - 1] := '';
if lnamels.Count > 0 then
begin
vgrid.DefaultColWidth := 80;
vgrid.DefaultRowHeight := 20;
vgrid.Options := [goFixedVertLine,goFixedHorzLine,goVertLine,goHorzLine,goRangeSelect,goRowSizing,goColSizing];
i := 1;
for j := 0 to lnamels.Count - 1 do
begin
if vgrid.ColCount < j then
vgrid.ColCount := vgrid.ColCount + 1;
vgrid.Cells[i,0] := lnamels.Strings[j];
Inc(i);
end;
llist := TsdNodeList.Create(False);
ls := TStringList.Create;
lnode.FindNodes(vkeynode, llist);
k := 1;
for i := 0 to llist.Count - 1 do
begin
lnode := llist[i];
ls.Clear;
GetXmlAllValues(ls,lnode);
if vgrid.RowCount < i then
vgrid.RowCount := vgrid.RowCount + 1;
for j := 0 to vgrid.ColCount - 1 do
begin
vgrid.Cells[j,k] := ls.Values[vgrid.Cells[j,0]];
end;
inc(k);
end;
FreeAndNil(llist);
FreeAndNil(ls);
Result := True;
end;
FreeAndNil(lnamels);
end;
FreeAndNil(lxml);
end;