unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, MSXML_TLB,Comobj, ComCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
tvXML: TTreeView;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure tvXMLClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
//创建
procedure TForm1.Button1Click(Sender: TObject);
var
doc : IXMLDOMDocument;
root,{child1,child2,}childtmp : IXMLDomElement;
//XMLAtri : IXMLDOMAttribute;
procedure addSysNode(pElement : IXMLDomElement;iDepth : Integer);
var
child1 : IXMLDomElement;
i : Integer;
begin
child1 := doc.createElement('SysNode'+inttostr(iDepth));
child1.setAttribute('Vol','102');
child1.setAttribute('Option','2');
child1.setAttribute('ConnectID','1');
child1.setAttribute('No','0');
child1.setAttribute('Num','-1');
pElement.appendChild(child1);
if iDepth = 5 then
begin
exit;
end;
addSysNode(child1,iDepth+1);
end;
begin
doc := CreateOLEObject('Microsoft.XMLDOM') as IXMLDomDocument;
root := doc.CreateElement('Site');
root.setAttribute('type','7');
doc.AppendChild(root);
childtmp := doc.CreateElement('SystemNode1');
childtmp.setAttribute('Option','2');
childtmp.setAttribute('ConnectID','1');
childtmp.setAttribute('No','0');
childtmp.setAttribute('Num','63');
root.AppendChild(childtmp);
childtmp := doc.CreateElement('SystemNode2');
childtmp.setAttribute('Option','2');
childtmp.setAttribute('ConnectID','1');
childtmp.setAttribute('No','0');
childtmp.setAttribute('Num','63');
root.AppendChild(childtmp);
childtmp := doc.createElement('SystemNode');
childtmp.setAttribute('Vol','102');
childtmp.setAttribute('Option','2');
childtmp.setAttribute('ConnectID','1');
childtmp.setAttribute('No','0');
childtmp.setAttribute('Num','-1');
root.AppendChild(childtmp);
addSysNode(childtmp,0);
doc.save('e:/xmltest.xml');
end;
//解析
procedure TForm1.Button2Click(Sender: TObject);
var
//i : integer;
doc : IXMLDOMDocument;
root : IXMLDOMNode;
tvRoot,curtvNode : TTreeNode;
TreeNodeList : TList;
atriArray : array[1..6] of String; //属性列表数组
{
**根据传入的属性名获取属性值
}
function getAtri(pNode : IXMLDOMNODE; atriName,atrOption : String):string;
var
tmpAtri1 : IXMLDOMNode;
begin
tmpAtri1 := pNode.attributes.getNamedItem(atriName);
if tmpAtri1 <> nil then
begin
if atrOption = '' then
begin
atrOption := tmpAtri1.nodeName+'='+tmpAtri1.nodeValue;
end
else
begin
atrOption := atrOption+','+tmpAtri1.nodeName+'='+tmpAtri1.nodeValue;
end;
end;
result := atrOption;
end;
{
**递归获取层次节点
}
procedure addNode(pNode : IXMLDOMNode;ptvNode : TTreeNode);
var
i : Integer;
tmpRoot,childtmp : IXMLDOMNode;
bBrother : Boolean;
atrOption : String;
begin
bBrother := False;
while(pNode <> nil) do
begin
atrOption := '';
for i := 1 to 6 do
begin
atrOption := getAtri(pNode,atriArray[i],atrOption);
end;
if not bBrother then
begin
ptvNode := tvXML.Items.AddChild(ptvNode,pNode.nodeName+'('+atrOption+')'); //父子节点
end
else
begin
ptvNode := tvXML.Items.Add(ptvNode,pNode.nodeName+'('+atrOption+')'); //兄弟节点
end;
if pNode.childNodes.length > 0 then
begin
childtmp := pNode.childNodes.item[0];
addNode(childtmp,ptvNode);
//ptvNode := TreeNodeList.
end;
pNode := pNode.nextSibling;
bBrother := True;
end;
end;
begin
doc := CreateOleObject('Microsoft.XMLDOM') as IXMLDomDocument;
doc.load('E:/xmltest.xml');
//属性名
atriArray[1] := 'type';
atriArray[2] := 'Vol';
atriArray[3] := 'Option';
atriArray[4] := 'ConnectID';
atriArray[5] := 'No';
atriArray[6] := 'Num';
root := doc.firstChild;
addNode(root,nil);
end;
procedure TForm1.tvXMLClick(Sender: TObject);
var
tvNode : TTreeNode;
begin
tvNode := tvXML.Selected;
if tvNode.Text = '2' then
exit;
end;
end.