How to create XML document in Delphi XE4 using IXMLDOCUMENT and IXMLNODE?

How to create XML document in Delphi XE4 using IXMLDOCUMENT and IXMLNODE?

Following is the self explanatory code in Delphi XE4 to create XML document using IXMLDOCUMENT and IXMLNODE interfaces. You should have XMLIntf and XmlDoc units in your uses section. Following CreateXMLinDelphi Delphi procedure creates an XML named MyXML in D drive.

procedure CreateXMLinDelphi;
var
  XML : IXMLDOCUMENT;
  RootNode, CurNode : IXMLNODE;
begin
  XML := NewXMLDocument;
  XML.Encoding := 'UTF-8';
  XML.Options := [doNodeAutoIndent];
  RootNode := XML.AddChild('ParentNode');
  CurNode := RootNode.AddChild('ChildNode1');
  CurNode.Text := 'ChildNode Text 1';
  CurNode := RootNode.AddChild('ChildNode2');
  CurNode.Text := 'ChildNode Text 2';
  XML.SaveToFile('D:\MyXML.xml');
  ShowMessage('XML created successfully');
end;

The above code generates following XML document:



  ChildNode Text 1
  ChildNode Text 2