Hola,
Hace cierto tiempo escribí un componente al que dí el original e imaginativo nombre de "XmlComoIni"... creo que puede servirte para hacerte una idea, por lo menos, de ciertos aspectos del componente "TXmlDocument".
Código Delphi
[-]
unit UXmlComoIni;
interface
uses
Classes, SysUtils, Graphics, XMLIntf, XMLDoc,
UTypesDecComp;
type
TXmlComoIni = class(TComponent)
private
FAcercade: TAcercade;
published
property Acercade: TAcercade read
FAcercade write FAcercade stored false;
private
FFileName: TFileName;
FDocXml: TXMLDocument;
procedure CrearDocXml;
procedure AbrirDocXml;
procedure GuardarDocXml;
public
constructor Create(AOwner: TComponent); reintroduce; overload; override;
constructor Create(AOwner: TComponent; const fileName: TFileName); reintroduce; overload;
destructor Destroy; override;
procedure UpdateFile;
procedure DeleteKey(const seccion, clave: string);
function SectionExists(const seccion: string) : boolean;
function KeyExists(const seccion, clave: string) : boolean;
procedure ReadSections(strings: TStrings);
procedure DeleteSection(const seccion: string);
procedure ReadSection(const seccion: string; strings: TStrings);
procedure ReadSectionValues(const seccion: string; strings: TStrings);
procedure WriteString(const seccion, clave, valor: string);
function ReadString(const seccion, clave, defecto: string): string;
procedure WriteInteger(const seccion, clave: string; const valor: integer);
function ReadInteger(const seccion, clave: string; const defecto: integer) : integer;
procedure WriteColor(const seccion, clave: string; const valor: TColor);
function ReadColor(const seccion, clave: string; const defecto: TColor) : integer;
procedure WriteBool(const seccion, clave: string; const valor: boolean);
function ReadBool(const seccion, clave: string; const defecto: boolean) : boolean;
procedure WriteDateTime(const seccion, clave: string; const valor: TDateTime);
function ReadDateTime(const seccion, clave: string; const defecto: TDateTime) : TDateTime;
procedure WriteFloat(const seccion, clave: string; valor: Double);
function ReadFloat(const seccion, clave: string; const defecto: Double) : Double;
end;
implementation
{$I ..\inc\rsDecComp.inc}
constructor TXmlComoIni.Create(AOwner: TComponent);
begin
inherited;
FFileName := rsFileName;
if not (csDesigning in ComponentState) then
begin
CrearDocXml;
FFileName := ChangeFileExt(ParamStr(0), rsExtXml);
AbrirDocXml;
end;
end;
constructor TXmlComoIni.Create(AOwner:
TComponent; const fileName: TFileName);
begin
CrearDocXml;
if (fileName <> EmptyStr) then
FFileName := fileName
else
FFileName := ChangeFileExt(ParamStr(0), rsExtXml);
AbrirDocXml;
end;
destructor TXmlComoIni.Destroy;
begin
if not (csDesigning in
ComponentState) then
GuardarDocXml;
FDocXml.Free;
inherited;
end;
procedure TXmlComoIni.CrearDocXml;
begin
FDocXml := TXMLDocument.Create(Self);
FDocXml.Options := [doNodeAutoIndent];
end;
procedure TXmlComoIni.GuardarDocXml;
begin
if FDocXml.Modified then
begin
FDocXml.SaveToFile(FFileName);
end;
end;
procedure TXmlComoIni.UpdateFile;
begin
GuardarDocXml;
end;
procedure TXmlComoIni.AbrirDocXml;
begin
if FileExists(FFileName) then
FDocXml.LoadFromFile(FFileName)
else
begin
FDocXml.Active := true;
FDocXml.Version := '1.0';
FDocXml.AddChild(rsRaizXml);
end;
end;
function TXmlComoIni.ReadString(const
seccion, clave, defecto: string): string;
var
nodoClave,
nodoSeccion: IXMLNode;
begin
Result := defecto;
with FDocXml.DocumentElement do
nodoSeccion := ChildNodes.FindNode(seccion);
if not Assigned(nodoSeccion) then
Exit; with nodoSeccion do
nodoClave := ChildNodes.FindNode(clave);
if not Assigned(nodoClave) then
Exit; Result := nodoClave.Text;
end;
procedure TXmlComoIni.WriteString(const
seccion, clave, valor: string);
var
nodoClave,
nodoSeccion: IXMLNode;
begin
if ReadString(seccion, clave, EmptyStr) = valor then
Exit; with FDocXml.DocumentElement do begin
nodoSeccion := ChildNodes.FindNode(seccion);
if not Assigned(nodoSeccion) then
nodoSeccion := AddChild(seccion);
end;
with nodoSeccion do begin
nodoClave := ChildNodes.FindNode(clave);
if not Assigned(nodoClave) then
nodoClave := AddChild(clave);
end;
nodoClave.Text := valor;
end;
function TXmlComoIni.ReadInteger(const seccion,
clave: string; const defecto: integer): integer;
begin
Result := StrToInt(ReadString(
seccion, clave, IntToStr(defecto)));
end;
procedure TXmlComoIni.WriteInteger(const seccion,
clave: string; const valor: integer);
begin
WriteString(seccion, clave, IntToStr(valor));
end;
function TXmlComoIni.ReadColor(const seccion,
clave: string; const defecto: TColor): integer;
begin
Result := StringToColor(ReadString(
seccion, clave, ColorToString(defecto)));
end;
procedure TXmlComoIni.WriteColor(const seccion,
clave: string; const valor: TColor);
begin
WriteString(seccion, clave, ColorToString(valor));
end;
function TXmlComoIni.ReadBool(const seccion,
clave: string; const defecto: boolean): boolean;
begin
Result := StrToBool(ReadString(
seccion, clave, BoolToStr(defecto)));
end;
procedure TXmlComoIni.WriteBool(const seccion,
clave: string; const valor: boolean);
begin
WriteString(seccion, clave, BoolToStr(valor));
end;
function TXmlComoIni.ReadDateTime(const seccion,
clave: string; const defecto: TDateTime): TDateTime;
begin
Result := StrToDateTime(ReadString(
seccion, clave, DateTimeToStr(defecto)));
end;
procedure TXmlComoIni.WriteDateTime(const seccion,
clave: string; const valor: TDateTime);
begin
WriteString(seccion, clave, DateTimeToStr(valor));
end;
function TXmlComoIni.ReadFloat(const seccion,
clave: string; const defecto: Double): Double;
begin
Result := StrToFloat(ReadString(
seccion, clave, FloatToStr(defecto)));
end;
procedure TXmlComoIni.WriteFloat(const
seccion, clave: string; valor: Double);
begin
WriteString(seccion, clave, FloatToStr(valor));
end;
procedure TXmlComoIni.DeleteKey(const
seccion, clave: string);
var
nodoClave,
nodoSeccion: IXMLNode;
begin
with FDocXml.DocumentElement do
nodoSeccion := ChildNodes.FindNode(seccion);
if Assigned(nodoSeccion) then
begin
nodoClave := nodoSeccion.ChildNodes.FindNode(clave);
if Assigned(nodoClave) then
nodoSeccion.ChildNodes.Delete(nodoClave.NodeName);
end;
end;
function TXmlComoIni.KeyExists(const
seccion, clave: string) : boolean;
var
nodoSeccion: IXMLNode;
begin
with FDocXml.DocumentElement do
nodoSeccion := ChildNodes.FindNode(seccion);
if Assigned(nodoSeccion) then
begin
with nodoSeccion do
Result := (ChildNodes.IndexOf(clave)<>-1);
end
else
Result := false;
end;
function TXmlComoIni.SectionExists(const
seccion: string) : boolean;
var
nodoSeccion: IXMLNode;
begin
with FDocXml.DocumentElement do
nodoSeccion := ChildNodes.FindNode(seccion);
Result := Assigned(nodoSeccion);
end;
procedure TXmlComoIni.ReadSectionValues(const
seccion: string; strings: TStrings);
var
i: integer;
nodoClave,
nodoSeccion: IXMLNode;
begin
with FDocXml.DocumentElement do
nodoSeccion := ChildNodes.FindNode(seccion);
if Assigned(nodoSeccion) then
begin
strings.Clear;
for i := 0 to nodoSeccion.ChildNodes.Count-1 do
begin
nodoClave := nodoSeccion.ChildNodes[i];
if Assigned(nodoClave) and
(nodoClave.IsTextElement) then
strings.Add(nodoClave.Text);
end;
end;
end;
procedure TXmlComoIni.ReadSections(strings: TStrings);
var
i: integer;
nodoSeccion: IXMLNode;
begin
nodoSeccion := FDocXml.DocumentElement;
if Assigned(nodoSeccion) then
begin
strings.Clear;
for i := 0 to nodoSeccion.ChildNodes.Count-1 do
strings.Add(nodoSeccion.ChildNodes[i].NodeName);
end;
end;
procedure TXmlComoIni.ReadSection(const
seccion: string; strings: TStrings);
var
i: integer;
nodoClave,
nodoSeccion: IXMLNode;
begin
with FDocXml.DocumentElement do
nodoSeccion := ChildNodes.FindNode(seccion);
if Assigned(nodoSeccion) then
begin
strings.Clear;
for i := 0 to nodoSeccion.ChildNodes.Count-1 do
begin
nodoClave := nodoSeccion.ChildNodes[i];
strings.Add(nodoClave.NodeName);
end;
end;
end;
procedure TXmlComoIni.DeleteSection(const seccion: string);
var
nodoSeccion: IXMLNode;
begin
nodoSeccion := FDocXml.DocumentElement.ChildNodes.FindNode(seccion);
if Assigned(nodoSeccion) then
FDocXml.DocumentElement.ChildNodes.Delete(nodoSeccion.NodeName);
end;
end.