lazarus的lpi等文件都是使用XML格式,使用Laz2_XMLCfg可以方便读写这类XML配置文件。
0、在uses添加Laz2_XMLCfg单元。
1、下面的例子是读取ProjectOptions/Units所有unit中FileName
<?xml version="1.0" encoding="UTF-8"?> <CONFIG> <ProjectOptions> <Units> <Unit> <Filename Value="project1.lpr"/> <IsPartOfProject Value="True"/> </Unit> <Unit> <Filename Value="unit1.pas"/> <IsPartOfProject Value="True"/> <ComponentName Value="Form1"/> <HasResources Value="True"/> <ResourceBaseClass Value="Form"/> <UnitName Value="Unit1"/> </Unit> <Unit> <Filename Value="unit2.pas"/> </Unit> </Units> </ProjectOptions> </CONFIG>
procedure TForm1.Button5Click(Sender: TObject); var XMLConfig: TXMLConfig; s:String; i,n,NewUnitCount:Integer; LegacyList: Boolean; SubPath: String; NewUnitFilename,Path: String; begin Memo1.Lines.Clear; Memo1.Lines.Add(''); Path:='ProjectOptions/'; XMLConfig:=TXMLConfig.Create('project2.lpi'); n:=XMLConfig.GetChildCount('ProjectOptions/Units'); LegacyList:= XMLConfig.IsLegacyList('ProjectOptions/Units/'); NewUnitCount:=XMLConfig.GetListItemCount('ProjectOptions/Units/', 'Unit', LegacyList); for i := 0 to NewUnitCount - 1 do begin SubPath:=Path+'Units/'+XMLConfig.GetListItemXPath('Unit', i, LegacyList)+'/'; NewUnitFilename:=XMLConfig.GetValue(SubPath+'Filename/Value',''); Memo1.Lines.Add(NewUnitFilename); end; XMLConfig.Flush; XMLConfig.Free; end;
2、在Units添加一下unit,FileName='Unit2.pas'
procedure TForm1.Button6Click(Sender: TObject); var XMLConfig: TXMLConfig; s:String; i,n,NewUnitCount:Integer; LegacyList: Boolean; SubPath: String; NewUnitFilename,Path: String; begin Path:='ProjectOptions/'; XMLConfig:=TXMLConfig.Create('project2.lpi'); n:=XMLConfig.GetChildCount(path+'Units'); LegacyList:= XMLConfig.IsLegacyList(path+'Units/'); NewUnitCount:=XMLConfig.GetListItemCount(path+'Units/', 'Unit', LegacyList);//读取Units的unit数量 //在原有的Units添加1个Unit i:=NewUnitCount; SubPath:=Path+'Units/'+XMLConfig.GetListItemXPath('Unit', i, LegacyList)+'/'; XMLConfig.SetValue(SubPath+'Filename/Value','unit2.pas'); XMLConfig.Flush; XMLConfig.Free; end;
3、删除第2个Unit
procedure TForm1.Button3Click(Sender: TObject); var XMLConfig: TXMLConfig; begin XMLConfig:=TXMLConfig.Create('project2.lpi'); XMLConfig.DeletePath('ProjectOptions/Units/Unit[2]'); XMLConfig.Flush; XMLConfig.Free; end;
4、删除第2个unit的FileName
procedure TForm1.Button2Click(Sender: TObject); var XMLConfig: TXMLConfig; begin XMLConfig:=TXMLConfig.Create('project2.lpi'); XMLConfig.DeleteValue('ProjectOptions/Units/Unit[2]/Filename/Value'); XMLConfig.Flush; XMLConfig.Free; end;
4、读第2个Unit的Filename
procedure TForm1.Button7Click(Sender: TObject); var XMLConfig: TXMLConfig; Path:String; begin Path:='ProjectOptions/'; XMLConfig:=TXMLConfig.Create('project2.lpi'); Memo1.Lines.Add(XMLConfig.GetValue(Path+'Units/Unit[2]/Filename/Value','')); XMLConfig.Flush; XMLConfig.Free; end;
标签:XMLConfig,Path,Laz2,lazarus,XMLCfg,Units,ProjectOptions,Unit,TXMLConfig From: https://www.cnblogs.com/qiufeng2014/p/18622328