创建和解析XML

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.

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值