unit Unit5;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, xmldom, XMLIntf, StdCtrls, msxmldom, XMLDoc, ComCtrls;
type
TForm5 =
class(TForm)
x: TXMLDocument;
Button1: TButton;
Button2: TButton;
TreeView1: TTreeView;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form5: TForm5;
implementation
{$R *.dfm}
Function GetNodeText(n:IXMLNode;forceText:Boolean=true;NeedsSep:Boolean=true):
String;
begin
if ForceText
then
begin
try
Result := n.Text
except
end;
end
else
begin
if n.IsTextElement
then Result := n.Text
else Result :='
';
end;
if NeedsSep
and (Length(Result)>0)
then Result := '
: ' + Result;
end;
Function Wrapped(
const s:
String):
String;
begin
if Length(s)>0
then Result := '
('+s+'
)'
else Result := s;
end;
Function GetKommaIfNeeded(
Const Exist:
String):
String;
begin
if length(Exist)>0
then Result := '
, '
else Result := '
';
end;
Function Translated(
Const instr:
String;Translate:Boolean):
String;
begin
Result := instr;
end;
Procedure ParseNodes(n:IXMLNode;tn:TTreeNode;Dest:TTreeView;Translate:Boolean);
var
i,j:Integer;
an:TTreeNode;
nodetext,att:
String;
begin
if Not (Assigned(n)
and Assigned(Dest))
then EXIT;
For i := 0
to n.ChildNodes.Count - 1
do
begin
att :='
';
for j := 0
to n.ChildNodes[i].AttributeNodes.Count - 1
do
att := Att + GetKommaIfNeeded(Att)+ Translated(n.ChildNodes[i].AttributeNodes[j].LocalName,Translate) + '
='+ n.ChildNodes[i].Attributes[n.ChildNodes[i].AttributeNodes[j].LocalName] ;
if Length(n.ChildNodes[i].LocalName)>0
then
begin
nodetext := Translated(n.ChildNodes[i].LocalName,Translate);
an := Dest.Items.AddChild(tn, nodetext + GetNodeText(n.ChildNodes[i],false,Length(nodetext)>0) + Wrapped(att));
ParseNodes(n.ChildNodes[i],an,dest,translate);
end;
end;
end;
procedure TForm5.Button2Click(Sender: TObject);
var
i:Integer;
fs:TFileStream;
begin
// x.XML.LoadFromFile('C:\temp\test.xml');
fs := TFileStream.Create('
C:\temp\test.xml',fmOpenRead);
fs.Position := 0;
try
x.XML.LoadFromStream(fs);
finally
fs.Free;
end;
x.Active := true;
ParseNodes(x.DocumentElement,
nil,Treeview1,true);
end;