delphi建立和讀取xml(xml簡單操做舉例)

有關許多接口,會須要經過xml進行數據交換,弄個例子,方便參考node

unit Unit1;orm

interfacexml

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,XMLDoc,XMLIntf, StdCtrls;接口

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;string

var
  Form1: TForm1;it

implementationio

{$R *.dfm}class

procedure TForm1.Button1Click(Sender: TObject);
var
Xml: TXMLDocument;
Rootnode,node : IXMLNode;
begin
Xml := TXMLDocument.Create(nil);
// Xml.LoadFromFile('aaa.xml');
try
//加入版本信息 ‘<?xml version="1.0" encoding="GB2312" ?> ’
Xml.Active := True;
Xml.Version := '1.0';
Xml.Encoding :='GB2312';coding

Rootnode := Xml.CreateNode('CIF_INFO');
Xml.DocumentElement := Rootnode;
node := Xml.CreateNode('TXNCODE');
Rootnode.ChildNodes.Add(node);
node.Text := '420001';
node := Xml.CreateNode('RESPCODE');
Rootnode.ChildNodes.Add(node);
node.Text := '0000';
node := Xml.CreateNode('CIFID');
Rootnode.ChildNodes.Add(node);
node.Text := '0030209728';
node := Xml.CreateNode('RESCODE');
Rootnode.ChildNodes.Add(node);
node.Text := '0000';
node := Xml.CreateNode('RESMSG');
Rootnode.ChildNodes.Add(node);
node.Text := '';List

Xml.SaveToFile('aaa.xml');
xml.Active := False;
showmessage('建立成功!');
finally
Xml.Free;
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
Xml: TXMLDocument;
Rootnode,node : IXMLNode;
nodelist:IXMLNodeList;
b,c,str :string;
i:Integer;
begin
Xml := TXMLDocument.Create(Application);


try
Xml.LoadFromFile('aaa.xml');
Xml.Active := True;

Rootnode := Xml.DocumentElement; nodeList :=Rootnode.ChildNodes;  for i:=0 to nodelist.Count-1 do     begin       if  Rootnode.ChildNodes[i].NodeValue = null then          begin            c:='空';          end          else          begin            c:= Rootnode.ChildNodes[i].NodeValue;          end;       str:=str+c+#13#10;     end; //b := Rootnode.ChildNodes[ 'CIFID'].NodeValue; Memo1.Lines.Add(str); xml.Active := False; finally Xml.Free; end; end; end.

相關文章
相關標籤/搜索