接口操作XML

接口操作XML

以下代码旨在 脱离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 @ 2019-07-17 20:21  delphi中间件  阅读(661)  评论(0编辑  收藏  举报