秋·风

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  278 随笔 :: 0 文章 :: 308 评论 :: 20万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
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;
复制代码

 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;
复制代码

 

 

posted on   秋·风  阅读(111)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
历史上的今天:
2020-12-22 将UnimDatePicker英文月份修改为1~12
点击右上角即可分享
微信分享提示