接口操作XML

接口操作XML

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
以下代码旨在 脱离TXMLDocument 操作 xml。
unit Unit3;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, xmldom, XMLIntf, msxmldom, XMLDoc, StdCtrls;
type
  TForm3 = class(TForm)
    XMLDocument1: TXMLDocument;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
  public
  end;
var
  Form3: TForm3;
implementation
uses eng;
{$R *.dfm}
procedure TForm3.Button1Click(Sender: TObject);
var xml:IXMLDocument;root,node,data:IXMLNode;i:Integer;
//  nt:TNodeType;
begin
  // 先来个中文版
  xml := NewXMLDocument;   // 默认 1.0
  xml.Options := xml.Options + [doNodeAutoIndent]; // 缩进格式,而不是全在一行
  xml.Encoding :='gb2312'; // 支持中文
  xml.DocumentElement := xml.CreateNode('根节点'); // ntElement 表示节点
  // 一样意思,只能含有一个根节点
//  xml.AddChild('根节点'); // NameSpaceURI 我理解,不用管,是一个命名空间的路径地址
  root := xml.DocumentElement;
  root.Attributes['说明'] := '怎样不用 TXMLDocument 控件操作 XML文件';
  root.Attributes['版权'] := '归 DuoSoft 所有';
  root.Attributes['i'] := '这里显示i的值';
  i := root.AttributeNodes.IndexOf('版权');
  if i <> -1 then
  begin
    root.AttributeNodes[i].Text := '还是归大家所有吧';
    root.AttributeNodes[i+1].NodeValue := i;
  end;
  node := xml.createElement_x_x_x('tag1','');
  node.Attributes['desc'] := '第1种添加节点方法';
  root.ChildNodes.Add(node);
  node := xml.CreateNode('tag2');
  node.Attributes['desc'] := '第2种添加节点方法';
  root.ChildNodes.Add(node);
  root.AddChild('tag3').Attributes['desc'] := '第3种添加节点方法';
  
  node := xml.CreateNode('备注',ntCData);
  node.Text := '1234'; // 备注 -> 1234
  root.ChildNodes.Add(node);
 
  node := xml.CreateNode('注释',ntComment);
  node.NodeValue := 5678; // 注释 -> 5678
  root.ChildNodes.Add(node);
  
for nt := ntReserved to ntNotation do
  begin
    i := Ord(nt);
    try
      node := xml.CreateNode('type'+IntToStr(i),nt);
      node.Attributes['desc'] := '类型'+IntToStr(i);
      root.ChildNodes.Add(node);
    except
//      node.Attributes['desc'] := '失败'+IntToStr(i);
      // 各自意义以后再研究★★★★★★★★★★★★★
//  TNodeType = (ntReserved, ntElement, ntAttribute, ntText, ntCData,
//    ntEntityRef, ntEntity, ntProcessingInstr, ntComment, ntDocument,
//    ntDocType, ntDocFragment, ntNotation);
    end;
  end;   }
  data := root.AddChild('数据清单');
  with data.AddChild('record') do
  begin
    Attributes['ID'] := 1;
    Attributes['Name'] := '张三';
    Attributes['sex'] := True;
    Attributes['Salary'] := 12.34;
  end;
  with data.AddChild('record') do
  begin
    Attributes['ID'] := 2;
    Attributes['Name'] := '李四';
    Attributes['sex'] := True;
    Attributes['Salary'] := 5678;
  end;
  with data.AddChild('record') do
  begin
    Attributes['ID'] := 3;
    Attributes['Name'] := '王五';
    Attributes['sex'] := false;
    Attributes['Salary'] := -90.1234;
  end;
  node := root.AddChild('备注');
  node.Attributes['年'] := 2010;
  node.Attributes['月'] := 7;
  node.Attributes['日'] := 12;
  node.Text := '建立了一份迷你表';
//  node.NodeName := 'NodeName'; // 不可修改
//  node.NodeValue := 'NodeValue'; // = node.Text
  // 以下代码很有趣!!!尽然合并到一起去了
  node.ChildNodes.Add(xml.CreateNode('再补充一个 ntText',ntText));
  i := node.AttributeNodes.Add(xml.CreateNode('时',ntAttribute));
  node.AttributeNodes[i].NodeValue := 16;
  node.AttributeNodes.Add(xml.CreateNode('分',ntAttribute));
  node.Attributes['分'] := 12;
//  node.AttributeNodes.Add(xml.CreateNode('秒',xxxx)); // 不支持 ntElement ntText ntComment 等其他类型
//  node.Attributes['秒'] := '还有秒?';
  xml.SaveToFile(ExtractFilePath(ParamStr(0))+'chn.xml');
{
结果如下
  <?xml version="1.0" encoding="gb2312" ?>
- <根节点 说明="怎样不用 TXMLDocument 控件操作 XML文件" 版权="还是归大家所有吧" i="1">
  <tag1 desc="第1种添加节点方法" />
  <tag2 desc="第2种添加节点方法" />
  <tag3 desc="第3种添加节点方法" />
- <数据清单>
  <record ID="1" Name="张三" sex="true" Salary="12.34" />
  <record ID="2" Name="李四" sex="true" Salary="5678" />
  <record ID="3" Name="王五" sex="false" Salary="-90.1234" />
  </数据清单>
  <备注 年="2010" 月="7" 日="12" 时="16" 分="12">建立了一份迷你表再补充一个 ntText</备注>
  </根节点>
}
end;
procedure TForm3.Button2Click(Sender: TObject);
var xml:IXMLDocument;root,node,data:IXMLNode;
begin
  // 节点变成英文的准备变成 接口
  xml := NewXMLDocument;   // 默认 1.0
  xml.Options := xml.Options + [doNodeAutoIndent]; // 缩进格式,而不是全在一行
  xml.Encoding :='gb2312'; // 支持中文
  root := xml.AddChild('DuoXMLRoot');
  root.Attributes['desc'] := '怎样不用 TXMLDocument 控件操作 XML文件';
  root.Attributes['right'] := '归 DuoSoft 所有';
  node := xml.createElement_x_x_x('tag1','');
  node.Attributes['desc'] := '第1种添加节点方法';
  root.ChildNodes.Add(node);
  node := xml.CreateNode('tag2');
  node.Attributes['desc'] := '第2种添加节点方法';
  root.ChildNodes.Add(node);
  root.AddChild('tag3').Attributes['desc'] := '第3种添加节点方法';
  data := root.AddChild('MyData'); // 注意这个名字,等下会用到
  with data.AddChild('record') do
  begin
    Attributes['ID'] := 1;
    Attributes['Name'] := '张三';
    Attributes['sex'] := True;
    Attributes['Salary'] := 12.34;
  end;
  with data.AddChild('record') do
  begin
    Attributes['ID'] := 2;
    Attributes['Name'] := '李四';
    Attributes['sex'] := True;
    Attributes['Salary'] := 5678;
  end;
  with data.AddChild('record') do
  begin
    Attributes['ID'] := 3;
    Attributes['Name'] := '王五';
    Attributes['sex'] := false;
    Attributes['Salary'] := -90.1234;
  end;
  node := root.AddChild('Memo');
  node.Attributes['yy'] := 2010;
  node.Attributes['mm'] := 7;
  node.Attributes['dd'] := 12;
  node.Text := '建立了一份迷你表';
  xml.SaveToFile(ExtractFilePath(ParamStr(0))+'eng.xml');
end;
  
好了,有一个 eng.xml 接下去还得需要一下 TXMLDocument
XMLDocument1.FileName 选择 eng.xml
双击控件打开 wizard
选择第一个节点,再选择第二个节点。
把 Document Element Type 勾上
next finish
生成一个 eng.pas
use进来
接下去的代码如下:直接利用接口的概念操作刚才我们生成的那个 xml
  
procedure TForm3.Button3Click(Sender: TObject);
var dd:IXMLDuoXMLRootType;
  i: Integer;
begin
  dd := LoadDuoXMLRoot(ExtractFilePath(ParamStr(0))+'eng.xml');
  dd.OwnerDocument.Options := dd.OwnerDocument.Options + [doAutoSave]; // 只要有修改就自动保存
  Memo1.Clear;
  for i := 0 to dd.MyData.Count - 1 do
  begin
    Memo1.Lines.Add('================='+inttostr(i)+'=================');
    Memo1.Lines.Add(IntToStr(dd.MyData[i].ID));
    Memo1.Lines.Add(dd.MyData[i].Name);
    Memo1.Lines.Add(dd.MyData[i].Sex);
    Memo1.Lines.Add(dd.MyData[i].Salary);
    if dd.MyData[i].ID = dd.MyData.Count then
    begin
      ShowMessage('改变最后一个值');
      dd.MyData[i].Salary := '123456789.0'; // 给你加薪
    end;
  end;
end;
end.

  

posted @   delphi中间件  阅读(666)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示