#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2016
    Posts
    1
    Rep Power
    0

    Thumbs up Generate an XML file in Delphi 10.1 and Perform basic Operations?


    I wanted to create an XML File in Delphi 10.1 in this Format
    Code:
    <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 this data from a Registration Form and when OK btn is clicked it should add the data from Edit Boxes,Radiobtns etc.. to XML File.

    Iam new to this Delphi Programming, Help me in sorting this out.

    I tried writing the Code in this way

    Code:
    procedure TfrmXmlFiles.btnOKClick(Sender: TObject);
    var
      XMLDoc_test: IXMLDocument;
      RootNode, RNode1, Node, childNode: IXMLNode;
    begin
      try
        XMLDoc_test := NewXMLDocument;
        XMLDoc_test.Options := [doNodeAutoIndent];
        RootNode := XMLDoc_test.AddChild('EmployeeDB');
        RNode1 := RootNode.AddChild('Employees');
        Node := RNode1.AddChild('Employee');
        Node.Attributes['e.id'] := edt_eID.Text;
        childNode := Node.AddChild('eName');
        childNode.Text := edtName.Text;
        childNode := Node.AddChild('ePlace');
        childNode.Text := edtPlace.Text;
    
        Node := RNode1.AddChild('Employee');
        Node.Attributes['e.id'] := edt_eID.Text;
        childNode := Node.AddChild('eName');
        childNode.Text := edtName.Text;
        childNode := Node.AddChild('ePlace');
        childNode.Text := edtPlace.Text;
    
        XMLDoc_test.XML.Text := XMLDoc.FormatXMLData(XMLDoc_test.XML.Text);
        XMLDoc_test.Active := true;
        XMLDoc_test.SaveToFile('nice-xml.xml');
        ResetForm;
      finally
        XMLDoc_test := nil;
      end;
    end;
    But it returned the xml file like below

    Code:
    <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>
    MyUI of the application is ike in this image:

    When i click on OK btn after entering the data, It should write the data to XML file , by creating a new <Employee/> node every time i click OK btn.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    397
    Rep Power
    0
    You explain that you want to add a new <Employee> to your <Employees> node every time the button is clicked. Well your code creates a NEW xml document and generates all the nodes every time you click the button. You need to create your xml document once and then add to it in the button click. It's up to you to determine when it's appropriate to create the xml document, if you know your form will absolutely create the xml file, then create it in the form's oncreate method:
    Code:
    type
      TfrmXmlFiles = class(TForm)
        ..
        procedure FormCreate(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
        procedure btnOKClick(Sender: TObject);
      private
        XMLDoc_test: IXMLDocument;
        EmployeeDBNode: IXMLNode;
        EmployeesNode: IXMLNode;
      public
        ..
      end;
    
    implementation
    
    procedure TfrmXmlFiles.FormCreate(Sender: TObject);
    begin
      XMLDoc_test := NewXMLDocument;
      XMLDoc_test.Options := [doNodeAutoIndent];
      XMLDoc_test.Active := True;
      EmployeeDBNode := XMLDoc_test.AddChild('EmployeeDB');
      EmployeesNode := EmployeeDBNode.AddChild('Employees');
    end;
    
    procedure TfrmXmlFiles.FormDestroy(Sender: TObject);
    begin
       XMLDoc_test := nil;
    end;
    
    procedure TfrmXmlFiles.btnOKClick(Sender: TObject);
    var
      NameNode: IXMLNode;
      PlaceNode: IXMLNode;
      EmployeeNode: IXMLNode;
    begin
      EmployeeNode := EmployeesNode.AddChild('Employee');
      EmployeeNode.Attributes['e.id'] := edt_eID.Text;
      NameNode := EmployeeNode.AddChild('eName');
      NameNode.Text := edtName.Text;
      PlaceNode := EmployeeNode.AddChild('ePlace');
      PlaceNode.Text := edtPlace.Text;
      XMLDoc_test.SaveToFile('nice-xml.xml');//this will probably put the file in the same directory
                                             //as the executable.  Not a good idea!!!
      ResetForm;//I assume a procedure you created to clear entries from form.
    end;

IMN logo majestic logo threadwatch logo seochat tools logo