lazarus的lpi等文件都是使用XML格式,使用Laz2_XMLCfg可以方便读写这类XML配置文件。
0、在uses添加Laz2_XMLCfg单元。
1、下面的例子是读取ProjectOptions/Units所有unit中FileName
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;
5、列出所有Unit节点的节点名称及节点的值:
procedure TForm1.Button9Click(Sender: TObject); var XMLConfig: TXMLConfig; s:String; i,j,n,NewUnitCount:Integer; LegacyList: Boolean; SubPath: String; Path: String; nd, nd2: TDOMNode; begin Path:='ProjectOptions/'; XMLConfig:=TXMLConfig.Create('project2.lpi'); NewUnitCount:=XMLConfig.GetListItemCount(path+'Units/', 'Unit', LegacyList);//读取Units的unit数量 for i := 0 to NewUnitCount - 1 do begin SubPath:=Path+'Units/'+XMLConfig.GetListItemXPath('Unit', i, LegacyList)+'/'; n:=XMLConfig.GetChildCount(SubPath); nd := XMLConfig.FindNode(SubPath, False); if nd <> nil then begin Memo1.Lines.Add(SubPath+':'); for j := 0 to nd.GetChildCount - 1 do begin nd2 := nd.ChildNodes[j]; s := nd2.NodeName; Memo1.Lines.Add(s+':'+XMLConfig.GetValue(SubPath+s+'/Value','')); end; Memo1.Lines.Add(''); End; end; end;
分类:
Lazarus
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
2020-12-22 将UnimDatePicker英文月份修改为1~12