마음의 안정을 찾기 위하여 - [Delphi] Use XML file as replacement for INI file in delphi
2079438
480
478
관리자새글쓰기
태그위치로그방명록
별일없다의 생각
dawnsea's me2day/2010
색상(RGB)코드 추출기(Color...
Connection Generator/2010
최승호PD, '4대강 거짓말 검...
Green Monkey**/2010
Syng의 생각
syng's me2DAY/2010
천재 작곡가 윤일상이 기획,...
엘븐킹's Digital Factory/2010
[Delphi] Use XML file as replacement for INI file in delphi
Delphi/Etc Tip | 2022/02/21 15:34

출처 : https://tech-story.net/use-xml-file-as-replacement-for-ini-file-in-delphi/


// Use XML file as replacement for INI file



{This code shows how to use TXMLDocument to save and restore configuration

settings in a XML document. The public methods works the same as a TIniFile.

There is not mutch comment in the code because it is self explaining

and small. Hope this benefit other persons. It is only tested in D7 pro.}



unit uCiaXml;



interface



uses

Forms, SysUtils, Windows, XmlIntf, XMLDoc;



type

TXMLConfig = class

private

FModified: Boolean;

FFileName: string;

FXMLDoc: TXMLDocument;

FBackup: Boolean;

function GetVersion: string;

public

constructor Create(const FileName: string); overload;

constructor Create; overload;

destructor Destroy; override;

procedure Save;

function ReadString(const Section, Key, default: string): string;

procedure WriteString(const Section, Key, Value: string);

function ReadInteger(const Section, Key: string; default: Integer): Integer;

procedure WriteInteger(const Section, Key: string; Value: Integer);

function ReadBoolean(const Section, Key: string; default: Boolean): Boolean;

procedure WriteBoolean(const Section, Key: string; Value: Boolean);

property Backup: Boolean read FBackup write FBackup;

property Version: string read GetVersion;

end;



implementation



{ TXMLConfig }



constructor TXMLConfig.Create(const FileName: string);

begin

inherited Create;

FBackup := True;

FFileName := FileName;

FXMLDoc := TXMLDocument.Create(Application);

FXMLDoc.Options := [doNodeAutoIndent];

if FileExists(FFileName) then

FXMLDoc.LoadFromFile(FFileName)

else

begin

FXMLDoc.Active := True;

FXMLDoc.AddChild(‘Configuration’);

end;

end;



constructor TXMLConfig.Create;

begin

Create(ChangeFileExt(Application.Exename, ‘_cfg.xml’));

end;



destructor TXMLConfig.Destroy;

begin

Save;

FXMLDoc.Destroy;

inherited;

end;



function TXMLConfig.GetVersion: string;

begin

Result := ‘1.00’;

end;



function TXMLConfig.ReadBoolean(const Section, Key: string; default: Boolean): Boolean;

begin

Result := Boolean(ReadInteger(Section, Key, Integer(default)));

end;



function TXMLConfig.ReadInteger(const Section, Key: string; default: Integer): Integer;

begin

Result := StrToInt(ReadString(Section, Key, IntToStr(default)));

end;



function TXMLConfig.ReadString(const Section, Key, default: string): string;

var

Node: IXMLNode;

begin

Node := FXMLDoc.DocumentElement.ChildNodes.FindNode(Section);

if Assigned(Node) and Node.HasAttribute(Key) then

Result := Node.Attributes[Key]

else

Result := default;

end;



procedure TXMLConfig.Save;

begin

if not FModified then

Exit;

if FBackup then



CopyFile(PChar(FFileName), PChar(FFileName + ‘.bak’), False);

FXMLDoc.SaveToFile(FFileName);

FModified := False;

end;



procedure TXMLConfig.WriteBoolean(const Section, Key: string; Value: Boolean);

begin

WriteInteger(Section, Key, Integer(Value));

end;



procedure TXMLConfig.WriteInteger(const Section, Key: string; Value: Integer);

begin

WriteString(Section, Key, IntToStr(Value));

end;



procedure TXMLConfig.WriteString(const Section, Key, Value: string);

var

Node: IXMLNode;

begin

if ReadString(Section, Key, ”) = Value then

Exit;

Node := FXMLDoc.DocumentElement.ChildNodes.FindNode(Section);

if not Assigned(Node) then

Node := FXMLDoc.DocumentElement.AddChild(Section);

Node.Attributes[Key] := Value;

FModified := True;

end;



end.
 // Use XML file as replacement for INI file


{This code shows how to use TXMLDocument to save and restore configuration

settings in a XML document. The public methods works the same as a TIniFile.

There is not mutch comment in the code because it is self explaining

and small. Hope this benefit other persons. It is only tested in D7 pro.}



unit uCiaXml;



interface



uses

Forms, SysUtils, Windows, XmlIntf, XMLDoc;



type

TXMLConfig = class

private

FModified: Boolean;

FFileName: string;

FXMLDoc: TXMLDocument;

FBackup: Boolean;

function GetVersion: string;

public

constructor Create(const FileName: string); overload;

constructor Create; overload;

destructor Destroy; override;

procedure Save;

function ReadString(const Section, Key, default: string): string;

procedure WriteString(const Section, Key, Value: string);

function ReadInteger(const Section, Key: string; default: Integer): Integer;

procedure WriteInteger(const Section, Key: string; Value: Integer);

function ReadBoolean(const Section, Key: string; default: Boolean): Boolean;

procedure WriteBoolean(const Section, Key: string; Value: Boolean);

property Backup: Boolean read FBackup write FBackup;

property Version: string read GetVersion;

end;



implementation



{ TXMLConfig }



constructor TXMLConfig.Create(const FileName: string);

begin

inherited Create;

FBackup := True;

FFileName := FileName;

FXMLDoc := TXMLDocument.Create(Application);

FXMLDoc.Options := [doNodeAutoIndent];

if FileExists(FFileName) then

FXMLDoc.LoadFromFile(FFileName)

else

begin

FXMLDoc.Active := True;

FXMLDoc.AddChild(‘Configuration’);

end;

end;



constructor TXMLConfig.Create;

begin

Create(ChangeFileExt(Application.Exename, ‘_cfg.xml’));

end;



destructor TXMLConfig.Destroy;

begin

Save;

FXMLDoc.Destroy;

inherited;

end;



function TXMLConfig.GetVersion: string;

begin

Result := ‘1.00’;

end;



function TXMLConfig.ReadBoolean(const Section, Key: string; default: Boolean): Boolean;

begin

Result := Boolean(ReadInteger(Section, Key, Integer(default)));

end;



function TXMLConfig.ReadInteger(const Section, Key: string; default: Integer): Integer;

begin

Result := StrToInt(ReadString(Section, Key, IntToStr(default)));

end;



function TXMLConfig.ReadString(const Section, Key, default: string): string;

var

Node: IXMLNode;

begin

Node := FXMLDoc.DocumentElement.ChildNodes.FindNode(Section);

if Assigned(Node) and Node.HasAttribute(Key) then

Result := Node.Attributes[Key]

else

Result := default;

end;



procedure TXMLConfig.Save;

begin

if not FModified then

Exit;

if FBackup then



CopyFile(PChar(FFileName), PChar(FFileName + ‘.bak’), False);

FXMLDoc.SaveToFile(FFileName);

FModified := False;

end;



procedure TXMLConfig.WriteBoolean(const Section, Key: string; Value: Boolean);

begin

WriteInteger(Section, Key, Integer(Value));

end;



procedure TXMLConfig.WriteInteger(const Section, Key: string; Value: Integer);

begin

WriteString(Section, Key, IntToStr(Value));

end;



procedure TXMLConfig.WriteString(const Section, Key, Value: string);

var

Node: IXMLNode;

begin

if ReadString(Section, Key, ”) = Value then

Exit;

Node := FXMLDoc.DocumentElement.ChildNodes.FindNode(Section);

if not Assigned(Node) then

Node := FXMLDoc.DocumentElement.AddChild(Section);

Node.Attributes[Key] := Value;

FModified := True;

end;



end.
2022/02/21 15:34 2022/02/21 15:34
Article tag list Go to top
View Comment 0
Trackback URL :: 이 글에는 트랙백을 보낼 수 없습니다
 
 
 
 
: [1][2][3][4][5][6][7][8] ... [862] :
«   2022/12   »
        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
전체 (862)
출판 준비 (0)
My-Pro... (41)
사는 ... (492)
블로그... (22)
My Lib... (32)
게임 ... (23)
개발관... (3)
Smart ... (1)
Delphi (75)
C Builder (0)
Object... (0)
VC, MF... (9)
Window... (1)
Open API (3)
Visual... (0)
Java, JSP (2)
ASP.NET (0)
PHP (4)
Database (12)
리눅스 (28)
Windows (23)
Device... (1)
Embedded (1)
게임 ... (0)
Web Se... (2)
Web, S... (19)
잡다한... (5)
프로젝트 (0)
Personal (0)
대통령... (9)
Link (2)