Hola:
Este es un ejemplo autoexplicativo de como Leer y Generar archivos en formato XML con Delphi 7 por medio del componente XMLDocument creado de forma dinámica:
Código Delphi
[-]
unit Unit26;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, xmldom, XMLIntf, msxmldom, XMLDoc;
type
TForm26 = class(TForm)
ListBox1: TListBox;
GroupBox1: TGroupBox;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
public
end;
var
Form26: TForm26;
implementation
{$R *.dfm}
procedure TForm26.Button1Click(Sender: TObject);
var
XML : TXMLDocument;
ChildNode : IXMLNode;
i, j : Integer;
begin
XML := TXMLDocument.Create(nil);
try
XML.Active := true;
XML.AddChild('Document_File_XML');
for i := 1 to 3 do
begin
ChildNode := XML.DocumentElement.AddChild('Root_Node-' + IntToStr(i));
for j:=1 to 3 do
ChildNode.AddChild('Child_Node-' + IntToStr(j)).text := 'TextChild-' + IntToStr(i) + IntToStr(j);
end;
XML.XML.Text := xmlDoc.FormatXMLData(XML.XML.Text);
XML.Active := true;
XML.SaveToFile('Document_File_XML.xml');
XML.Active := False;
finally
XML := nil;
end;
end;
procedure TForm26.Button2Click(Sender: TObject);
var
XML : TXMLDocument;
ChildNode : IXMLNode;
i,j : Integer;
begin
ListBox1.Clear;
XML := TXMLDocument.Create(Self);
try
XML.Active := True;
XML.LoadFromFile(ExtractFilePath(Application.ExeName) + 'Document_File_XML.xml');
for i:= 0 to XML.DocumentElement.ChildNodes.Count - 1 do
begin
ChildNode := XML.DocumentElement.ChildNodes[i];
for j:= 1 to 3 do
ListBox1.Items.Add(ChildNode.ChildNodes['Child_Node-' + IntToStr(j)].Text);
end;
XML.Active := False;
finally
XML := nil;
end;
end;
end.
Código:
// Archivo Document_File_XML Creado y Leído por las rutinas anteriores
<Document_File_XML>
<Root_Node-1>
<Child_Node-1>TextChild-11</Child_Node-1>
<Child_Node-2>TextChild-12</Child_Node-2>
<Child_Node-3>TextChild-13</Child_Node-3>
</Root_Node-1>
<Root_Node-2>
<Child_Node-1>TextChild-21</Child_Node-1>
<Child_Node-2>TextChild-22</Child_Node-2>
<Child_Node-3>TextChild-23</Child_Node-3>
</Root_Node-2>
<Root_Node-3>
<Child_Node-1>TextChild-31</Child_Node-1>
<Child_Node-2>TextChild-32</Child_Node-2>
<Child_Node-3>TextChild-33</Child_Node-3>
</Root_Node-3>
</Document_File_XML>
Se agradece cualquier información adicional sobre el Componente XMLDocument
Espero sea de utilidad este sencillo ejemplo genérico el cual puede ser adaptado a situaciones particulares
Nelson