黄聪:Delphi 中的 XMLDocument 类详解(8) - 添加与删除节点
2010-08-27 11:18
597 查看
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, xmldom, XMLIntf, msxmldom, XMLDoc, StdCtrls;
type
TForm1 = class(TForm)
XMLDocument1: TXMLDocument;
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
//打开
procedure TForm1.FormCreate(Sender: TObject);
begin
XMLDocument1.LoadFromFile('c:\temp\test.xml');
{必须用万一提供的 xml 测试文件, 才能有相同的返回值}
end;
//添加
procedure TForm1.Button1Click(Sender: TObject);
var
newNode, newNodeAdd: IXMLNode;
begin
newNode := XMLDocument1.DocumentElement.AddChild('人员');
newNode.Attributes['职务'] := '秘书';
newNodeAdd := newNode.AddChild('姓名');
newNodeAdd.Text := '小翠';
newNodeAdd := newNode.AddChild('性别');
newNodeAdd.Text := '女';
Memo1.Lines := XMLDocument1.XML; {查看}
{会在尾部增加:
<人员 职务="秘书"><姓名>小翠</姓名><性别>女</性别></人员>
}
end;
//删除
procedure TForm1.Button2Click(Sender: TObject);
var
nodeList: IXMLNodeList;
begin
nodeList := XMLDocument1.DocumentElement.ChildNodes;
nodeList.Delete(nodeList.Count-1); {删除最后一条}
Memo1.Lines := XMLDocument1.XML; {查看}
end;
end.
-------------------------------------------------------------------------------------------------------------------------------
出处:http://www.cnblogs.com/del/archive/2008/01/03/1024681.html
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, xmldom, XMLIntf, msxmldom, XMLDoc, StdCtrls;
type
TForm1 = class(TForm)
XMLDocument1: TXMLDocument;
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
//打开
procedure TForm1.FormCreate(Sender: TObject);
begin
XMLDocument1.LoadFromFile('c:\temp\test.xml');
{必须用万一提供的 xml 测试文件, 才能有相同的返回值}
end;
//添加
procedure TForm1.Button1Click(Sender: TObject);
var
newNode, newNodeAdd: IXMLNode;
begin
newNode := XMLDocument1.DocumentElement.AddChild('人员');
newNode.Attributes['职务'] := '秘书';
newNodeAdd := newNode.AddChild('姓名');
newNodeAdd.Text := '小翠';
newNodeAdd := newNode.AddChild('性别');
newNodeAdd.Text := '女';
Memo1.Lines := XMLDocument1.XML; {查看}
{会在尾部增加:
<人员 职务="秘书"><姓名>小翠</姓名><性别>女</性别></人员>
}
end;
//删除
procedure TForm1.Button2Click(Sender: TObject);
var
nodeList: IXMLNodeList;
begin
nodeList := XMLDocument1.DocumentElement.ChildNodes;
nodeList.Delete(nodeList.Count-1); {删除最后一条}
Memo1.Lines := XMLDocument1.XML; {查看}
end;
end.
-------------------------------------------------------------------------------------------------------------------------------
出处:http://www.cnblogs.com/del/archive/2008/01/03/1024681.html
相关文章推荐
- Delphi 中的 XMLDocument 类详解(8) - 添加与删除节点
- 黄聪:Delphi 中的 XMLDocument 类详解(6) - 访问节点属性
- 黄聪:Delphi 中的 XMLDocument 类详解(7) - 获取节点名称与节点的属性名称
- 黄聪:Delphi 中的 XMLDocument 类详解(10) - 判断节点类型: 支节点、叶节点、文本节点、空节点
- 黄聪:Delphi 中的 XMLDocument 类详解(16) - 节点列表中的第一个与最后一个节点
- 黄聪:Delphi 中的 XMLDocument 类详解(17) - 上一个节点、下一个节点、父节点
- 黄聪:Delphi 中的 XMLDocument 类详解(19) - NodeValue 与 NodeName 的读写区别
- Delphi 中的 XMLDocument 类详解(10) - 判断节点类型: 支节点、叶节点、文本节点、空节点
- Delphi 中的 XMLDocument 类详解(16) - 节点列表中的第一个与最后一个节点
- 黄聪:Delphi 中的 XMLDocument 类详解(20) - 动态建立 XMLDocument 对象
- Delphi 中的 XMLDocument 类详解(7) - 获取节点名称与节点的属性名称
- Delphi 中的 XMLDocument 类详解(17) - 上一个节点、下一个节点、父节点
- c++ builder 中的 XMLDocument 类详解(8) -添加与删除结点
- 黄聪:Delphi 中的 XMLDocument 类详解(1) - 等待研究的内容
- 黄聪:Delphi 中的 XMLDocument 类详解(9) - 关于 HasChildNodes 与 IsTextElement
- c++ builder 中的 XMLDocument 类详解(8) -添加与删除结点
- 黄聪:Delphi 中的 XMLDocument 类详解(2) - 记要
- 黄聪:Delphi 中的 XMLDocument 类详解(3) - 读取 xml 文件
- 黄聪:Delphi 中的 XMLDocument 类详解(11) - 一个多余的属性: Nodes
- Delphi 中的 XMLDocument 类详解(6) - 访问节点属性