How to create XML document in Delphi XE4 using IXMLDOCUMENT and IXMLNODE?
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:
<?xml version="1.0" encoding="UTF-8"?>
<ParentNode>
<ChildNode1>ChildNode Text 1</ChildNode1>
<ChildNode2>ChildNode Text 2</ChildNode2>
</ParentNode>
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:
<?xml version="1.0" encoding="UTF-8"?>
<ParentNode>
<ChildNode1>ChildNode Text 1</ChildNode1>
<ChildNode2>ChildNode Text 2</ChildNode2>
</ParentNode>
No comments:
Post a Comment