I wanted to create an XML file in Delphi 10.1 using following format
<EmployeeDB>
<Employees>
<Employee e.id="1">
<eName>value from Edit Box edtName</eName>
<ePlace>value from Edit Box edtPlace</ePlace>
</Employee >
<Employee e.id="2">
<eName>value from Edit Box edtName</eName>
<ePlace>value from Edit Box edtPlace</ePlace>
</Employee >
<Employee e.id="3">
<eName>value from Edit Box edtName</eName>
<ePlace>value from Edit Box edtPlace</ePlace>
</Employee >
</Employees>
</EmployeeDB>
I wanted to take data from a registration form and when the OK button is clicked it should add the data from Edit Boxes, RadioButtons etc.. to XML file.
I am new to Delphi Programming, help me sorting this out.
I tried writing the code in this way:
unit XMLTrail;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, XMLIntf, XMLDoc, ComObj, xmldom,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
Tfrm_XMLTrail = class(TForm)
edt_eID: TEdit;
edtName: TEdit;
edtPlace: TEdit;
Memo1: TMemo;
btnAdd: TButton;
procedure btn_AddClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
procedure LoadXML;
procedure SaveXML;
public
{ Public declarations }
XMLDoc1: TXmlDocument;
iNode, Root1, Root2, Child_Attrib_Name, Child_Attrib_Place: IXmlNode;
function GetEmployeeDBNode(XMLDoc: TXmlDocument): IXmlNode;
function GetEmployeesNode(EmployeeDBNode: IXmlNode): IXmlNode;
end;
var
frm_XMLTrail: Tfrm_XMLTrail;
implementation
{$R *.dfm}
const
scXmlTemplate = '<EmployeeDB>'#13#10 + ' <Employees>'#13#10 +
' </Employees>'#13#10 + '</EmployeeDB>';
scXmlFileName =
'C:\Users\Rajesh\Documents\Embarcadero\Studio\Projects\Samples\XML trail\Win32\Debug\nicexml.xml';
procedure Tfrm_XMLTrail.btn_AddClick(Sender: TObject);
begin
XMLDoc1.Active := true;
iNode := XMLDoc1.DocumentElement;
Root1 := iNode.ChildNodes.FindNode('Employees');
Root2 := Root1.AddChild('Employee');
Root2.Attributes['e.id'] := edt_eID.Text;
Child_Attrib_Name := Root2.AddChild('eName');
Child_Attrib_Name.Text := edtName.Text;
Child_Attrib_Place := Root2.AddChild('ePlace');
Child_Attrib_Place.Text := edtPlace.Text;
XMLDoc1.Active := true;
XMLDoc1.SaveToFile(scXmlFileName);
end;
procedure Tfrm_XMLTrail.FormCreate(Sender: TObject);
begin
XMLDoc1 := TXmlDocument.Create(nil);
if not FileExists(scXmlFileName) then
begin
// XMLDoc1 := TXmlDocument.Create(nil);
XMLDoc1.Active := true;
XMLDoc1.Options := [doNodeAutoIndent];
iNode := XMLDoc1.AddChild('UmangEmployeeDB');
Root1 := iNode.AddChild('Employees');
end
else
begin
LoadXML;
end;
end;
procedure Tfrm_XMLTrail.LoadXML;
begin
XMLDoc1.LoadFromFile(scXmlFileName);
Memo1.Lines.Text := XMLDoc1.XML.Text;
// SaveXML;
// Memo1.Lines.LoadFromFile(scXmlFileName);
// XMLDoc1 := TXmlDocument.Create(nil);
// Assert(Root1 <> Nil);
end;
procedure Tfrm_XMLTrail.SaveXML;
begin
Memo1.Lines.SaveToFile(scXmlFileName);
end;
end.
But it returned the XML file like below:
<EmployeeDB>
<Employees>
<Employee e.id="2">
<eName>sssss</eName>
<ePlace>fgr</ePlace>
</Employee>
<Employee e.id="2">
<eName>sssss</eName>
<ePlace>fgr</ePlace>
</Employee>
</Employees>
</EmployeeDB>
The UI of the application is like in this Image.
When I click on OK button after entering the data, it should write the data to XML file by creating a new <Employee/> node every time I click OK button.
<EmployeeDB> <Employees> <Employee e.id="1"> <eName>value from Edit Box edtName</eName> <ePlace>value from Edit Box edtPlace</ePlace> </Employee > <Employee e.id="2"> <eName>value from Edit Box edtName</eName> <ePlace>value from Edit Box edtPlace</ePlace> </Employee > <Employee e.id="3"> <eName>value from Edit Box edtName</eName> <ePlace>value from Edit Box edtPlace</ePlace> </Employee > </Employees> </EmployeeDB>