利用第三方-NativeXML控件来实现XML文件的读、查功能 完整代码如下:
----------------------------(很多测试代码未删除 均已//注释)
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ExtCtrls, Menus;
type
TForm1 = class(TForm)
Memo1: TMemo;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Button2: TButton;
GroupBox1: TGroupBox;
Edit4: TEdit;
Label5: TLabel;
GroupBox2: TGroupBox;
Label4: TLabel;
GroupBox3: TGroupBox;
BitBtn1: TBitBtn;
Bevel1: TBevel;
PopupMenu1: TPopupMenu;
N1: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
N4: TMenuItem;
N5: TMenuItem;
N6: TMenuItem;
N7: TMenuItem;
BitBtn3: TBitBtn;
BitBtn4: TBitBtn;
BitBtn2: TBitBtn;
procedure Button1Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Edit4KeyPress(Sender: TObject; var Key: Char);
procedure N1Click(Sender: TObject);
procedure N4Click(Sender: TObject);
procedure N2Click(Sender: TObject);
procedure BitBtn4Click(Sender: TObject);
procedure BitBtn3Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses NativeXML;
procedure TForm1.Button1Click(Sender: TObject);
begin
end;
// [清空]模块
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
Memo1.Lines.Clear ;
Label4.Caption :='' ;
edit4.Text :='' ;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
end;
//edit回车功能
procedure TForm1.Edit4KeyPress(Sender: TObject; var Key: Char);
begin
if key=#13 then
BitBtn4Click(nil);
end;
procedure TForm1.N1Click(Sender: TObject);
begin
BitBtn3Click(nil) ;
end;
procedure TForm1.N4Click(Sender: TObject);
begin
BitBtn1Click(nil) ;
end;
procedure TForm1.N2Click(Sender: TObject);
begin
BitBtn4Click(nil);
end;
// [查找]功能部分
procedure TForm1.BitBtn4Click(Sender: TObject);
var
ADoc: TNativeXml;
i,j,m,n:integer;
nd1,nd2 :TXMLNode;
begin
Memo1.Lines.Clear;
ADoc := TNativeXml.Create;
try
ADoc.LoadFromFile('E:/Signal.xml');
ADoc.XmlFormat := xfReadable;
m:=Adoc.Root.NodeCount ;
n:=0;
for m:=0 to m-1 do
begin
j:=Adoc.Root.Nodes[m].NodeCount ;
nd1 :=Adoc.Root.Nodes[m] ;
for i:=0 to j-1 do
begin
nd2 :=nd1.Nodes[i] ;
if nd1.Name =trim(edit4.Text ) then
with Memo1.Lines do
begin
Add('ID= '+nd1.AttributeByName['Id']);
Add('Name= '+nd1.AttributeByName['Name']);
Add('ID= '+nd2.AttributeByName['id']);
Add('Name= '+nd2.AttributeByName['name']);
Add('Signal= '+nd2.AttributeByName['signal']);
Add('-----------------------------------------');
n :=n+1 ;
end
else if nd2.Name =trim(edit4.Text ) then
with Memo1.Lines do
begin
Add('ID= '+nd2.AttributeByName['id']);
Add('Name= '+nd2.AttributeByName['name']);
Add('Signal= '+nd2.AttributeByName['signal']);
Add('----------------------------------------');
n :=n+1 ;
end;
end;
end;
Label4.Caption :='数据记录总计: '+inttostr(n)+' 条 ';
ShowMessage('查找完成!');
finally
Adoc.Free ;
end;
end;
// [ 读取]功能模块
procedure TForm1.BitBtn3Click(Sender: TObject);
var
ADoc: TNativeXml;
i,j,m,n:integer;
nd1, nd2: txmlNode;
begin
Memo1.Lines.Clear;
ADoc := TNativeXml.Create;
try
ADoc.LoadFromFile('E:/Signal.xml');
ADoc.XmlFormat := xfReadable;
//j:=Adoc.Root.FindNode('item').NodeCount ;
n:=0 ;
m:=Adoc.Root.NodeCount ;
//showmessage(inttostr(j));
for m:=0 to m-1 do
begin
j:=Adoc.Root.Nodes[m].NodeCount ;
nd1 := adoc.Root.Nodes[m]; //item
for i:=0 to j-1 do
begin
nd2 := nd1.Nodes[i]; //define
Memo1.Lines.Add('ID= '+nd2.AttributeByName['id']);
Memo1.Lines.Add('Name= '+nd2.AttributeByName['name']);
Memo1.Lines.Add('Signal= '+nd2.AttributeByName['signal']);
Memo1.Lines.Add('-----------------');
n:=n+1;
end;
end;
Label4.Caption :='数据记录总计: '+inttostr(n)+' 条 ' ;
showmessage('数据读取完成!');
finally
ADoc.Free;
end;
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
Form1.Free ;
end;
end.
----------------------------
相关XML文件格式:
<SignalDefine>
<Item Id="1" Name="大车连续障碍" >
<Define id="1" name="LXZI_L_01" Signal="$00000001" />
...
<Define id="21" name="辅助信号" Signal="$0080000000" />
</Item>
<Item Id="2" Name="大车限宽门" >
<Define id="1" name="XKM_L_01" Signal="$00000001" />
...
<Define id="11" name="XKM_P_06" Signal="$00200000" />
</Item>
<Item Id="3" Name="大车坡道起步" >
<Define id="1" name="PDQB_L_01" Signal="$00000010" />
...
<define id="169" name="ER_B_83" Signal="$CB000000" reverse="0" />
<define id="170" name="ER_B_84" Signal="$CC000000" reverse="0" />
</Item>
<Item Id="4" Name="大车单边桥" >
<Define id="1" name="DBQ_L_01" Signal="$00000001" />
...
<Define id="10" name="DBQ_L_10" Signal="$00000200" />
</Item>
<Item Id="5" Name="大车直角转弯">
<Define id="1" name="ZJZW_L_01" Signal="$00000400" />
...
<Define id="5" name="ZJZW_P" Signal="$00004000" />
</Item>
<Item Id="6" Name="大车曲线行驶">
<Define id="1" name="QXXS_L_01" Signal="$00010000" />
...
<Define id="4" name="QXXS_L_04" Signal="$00080000" />
</Item>
</SignalDefine>
------------------------------
2009-11-04