基于SAX的,试了一下,相当好用
procedure TForm1.DomView(Document: IDocument; View: TTreeView);
procedure ParseXMLNode(DataNode: IElement; TreeNode: TTreeNode);
var
I: IIterator;
N: TTreeNode;
Child: IElement;
Attr: TStrings;
S: string;
begin
I := DataNode.Children.Iterator;
while i.HasNext do
begin
if 0 = (I.Next as INode).QueryInterface(IElement, child) then
begin
Attr := XMLAttsToStrings(Child);
try
S := Format('%s[%s]', [Child.name, Attr.CommaText]);
N := View.Items.AddChild(TreeNode, S);
ParseXMLNode(child, N)
finally
Attr.Free;
end;
end;
end;
end;
begin
ParseXMLNode(FDocument.root, View.TopItem);
end;
function XMLAttsToStrings(Node: IElement): TStrings;
var
i :IIterator;
child: JALMiniDOM.INode;
text : JALMiniDOM.ITextNode;
s : string;
begin
Result := TStringList.Create;
try
i := Node.Attributes.Iterator;
while i.HasNext do
begin
with i.Next as IAttribute do
begin
if Value <> '' then
Result.Values[Name] := Value
else
Result.Add(Name + '=');
end;
end;
s := '';
i := Node.Children.Iterator;
while i.HasNext do
begin
child := i.next as INode;
if 0 = child.QueryInterface(ITextNode, text) then
s := s + text.text;
end;
s := TrimRight(s);
if s <> '' then
Result.Values['text'] := s;
except
FreeAndNil(Result);
raise;
end;
end;