unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, xmldom, XMLIntf, msxmldom, XMLDoc;
type
TForm1 = class(TForm)
mmo1: TMemo;
btnCreateXML: TButton;
edt1: TEdit;
XMLDoc1: TXMLDocument;
btnReadXML: TButton;
btnSetAttr: TButton;
btnReadAttribute: TButton;
btnDelNode: TButton;
procedure btnCreateXMLClick(Sender: TObject);
procedure btnReadXMLClick(Sender: TObject);
procedure btnSetAttrClick(Sender: TObject);
procedure btnReadAttributeClick(Sender: TObject);
procedure btnDelNodeClick(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.btnCreateXMLClick(Sender: TObject);
var
aNode, aValueNode: IXMLNode;
wStr: widestring;
begin
//创建XML文件
with XMLDoc1.XMLdo
begin
Add('<?xml version="1.0"
standalone="yes"?>');
Add('<DFW>');
Add('<savenight>');
Add('<name>savenight</name>');
Add('<sex>man</sex>');
Add('</savenight>');
Add('</DFW>');
end;
XMLDoc1.Active := true;
XMLDoc1.SaveToFile('test.xml');
//WRITE
aNode := XMLDoc1.DocumentElement.AddChild('xiey');
aValueNode := aNode.AddChild('name');
aValueNode.Text := 'xiey';
aValueNode := aNode.AddChild('sex');
aValueNode.Text := 'man or woman?';
//READ
aNode := XMLDoc1.DocumentElement.ChildNodes[1];
wStr := aNode.ChildNodes['name'].Text;
ShowMessage('xiey'' name is :' + wStr);
wStr := aNode.ChildNodes['sex'].Text;
ShowMessage('xiey'' sex is :' + wStr);
end;
procedure TForm1.btnSetAttrClick(Sender: TObject);
//设置属性
var
aNode, aValueNode: IXMLNode;
wStr: WideString;
i, j: Integer;
begin
xmldoc1.LoadFromfile('test.xml');
aNode := XMLDoc1.DocumentElement.AddChild('Happy');
with aNodedo
begin
NodeValue := 'Have fun';
SetAttributeNS('Attr', '', 'attrValue');
SetAttributeNS('BodyColor', '', 'Black');
SetAttributeNS('Age', '', 12);
end;
// aNode.AddChild('HappyChild');
// anode.ChildNodes[0].NodeValue:='I am Happy child';
XMLDoc1.DocumentElement.AddChild('WhyNot');
XMLDoc1.SaveToFile('test.xml');
end;
procedure TForm1.btnReadXMLClick(Sender: TObject);
//读取XML文件
var
aNode, aValueNode: IXMLNode;
wStr: widestring;
i, j: Integer;
begin
XMLDoc1.LoadFromFile('TEST.XML');
for i := 0 to XMLDoc1.DocumentElement.ChildNodes.count - 1do
begin
aNode := XMLDoc1.DocumentElement.ChildNodes;
if aNode.HasChildNodes = false then
mmo1.Lines.Add(aNode.NodeName);
for j := 0 to XMLDoc1.DocumentElement.ChildNodes.ChildNodes.Count - 1do
begin
aNode := XMLDoc1.DocumentElement.ChildNodes.ChildNodes[j];
if aNode.NodeName = 'name' then
mmo1.lines.Add(aNode.NodeName + ' : ' + aNode.NodeValue);
end;
end;
end;
procedure TForm1.btnReadAttributeClick(Sender: TObject);
//读取属性
var
i, j: Integer;
aNode, aValueNode: IXMLNode;
begin
mmo1.Clear;
xmldoc1.LoadFromFile('yes.xml');
XMLDoc1.Active;
j := xmlDoc1.DocumentElement.ChildNodes.Count - 1;
// ShowMessage(IntToStr(j) + ':' + xmlDoc1.DocumentElement.NodeName);
for i := 0 to jdo
begin
aNode := xmlDoc1.DocumentElement.ChildNodes;
if aNode.NodeType = ntElement then
begin
avaluenode := aNode;
with mmo1.Linesdo
begin
Add(avalueNode.NodeName);
add(aNode.Attributes['Name']);
Add(aNode.Attributes['ServerName']);
Add(aNode.Attributes['UserName']);
Add(aNode.Attributes['Password']);
Add(aNode.Attributes['Trusted']);
Add('---------------------------');
end;
end;
end;
XMLDoc1.Active := false;
end;
procedure TForm1.btnDelNodeClick(Sender: TObject);
//删除指定节点
begin
xmldoc1.LoadFromFile('yes.xml');
xmlDoc1.DocumentElement.ChildNodes.Delete(edt1.text);
XMLDoc1.SaveToFile('yes.xml');
btnReadAttributeClick(Sender);
end;
end.
----------Yes.xml内容如下---------------------
<?xml version="1.0"
standalone="yes"?>
<eLibrary>
<Profiw Name="Dfjphi"
ServerName="(losfsdfscal)"
UserName="safa"
Password="EncrytPassword"
Trusted="TRUE"/>
<Profie Name="Dhfphi"
ServerName="(lsdfsdocal)"
UserName="sga"
Password="Enc"
Trusted="TRUE"/>
<Profir Name="Dfgphi"
ServerName="(lofsdfcal)"
UserName="sga"
Password="Ensword"
Trusted="TRUE"/>
</eLibrary>