秋·风

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
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 2024-12-22 17:50  秋·风  阅读(169)  评论(0)    收藏  举报