Creating XML documents with OpenXML
Posted: (EET/GMT+2)
Creating XML documents with OpenXML
Dec 17, 2000
Borland Delphi 5 & OpenXML 2.3.3
If you are using Delphi, there aren't yet any built-in features for parsing and creating XML documents, if not counting up MIDAS. However, the Internet usually comes to the rescue, and so does it this time, too. From www.philo.de/xml you can find a component called OpenXML (or Open XML), which is an open-source XML parsing component for Delphi.
The component installs neatly into the Delphi IDE (it creates a XML component palette tab) and provides two components. The OpenXML component supports the XML DOM, just like the Microsoft MSXML component does. There are some slight differences in these two implementations, and the MSXML seems to be more professional, but still the OpenXML component is very usable indeed.
As an additional plus the OpenXML component is easy to deploy, whereas the MSXML component requires at least Internet Explorer 5.0 or later installed on the client computer.
Creating XML with OpenXML
Once you have the OpenXML components installed, your component palette should have a new XML tab:

The image was taken after installing the component version 2.3.3, being dated 13th of Dec, 2000. To test the them, drop a TDomImplementation component, a button, and a TMemo onto a form:

Then, write the following code to the OnClick event handler of the button:
procedure TForm1.Button1Click(Sender: TObject);
Var
DD : TDOMDocument;
Procedure QuickElementCreate(Element,Value : String);
Var TE : TDOMElement;
Begin
TE := DD.CreateElement(Element);
TE.AppendChild(DD.CreateTextNode(Value));
DD.DocumentElement.AppendChild(TE);
End;
begin
With DomImplementation1 do Begin
DD := CreateDocument('CUSTOMER',nil);
QuickElementCreate('NAME','John Doe');
QuickElementCreate('ADDRESS','2330 Pine Blvd');
QuickElementCreate('PHONE','+358 (0)9 112 8825');
Memo1.Lines.Text := DD.CodeAsString;
FreeDocument(DD);
End;
end;
When run, this code causes the following XML code to be created (code beautified):
<?xml version="1.0" encoding="UTF-8"?> <CUSTOMER> <NAME>John Doe</NAME> <ADDRESS>2330 Pine Blvd</ADDRESS> <PHONE>+358 (0)9 112 8825</PHONE> </CUSTOMER>

As you can see, using the OpenXML component is very easy! But be aware of the fact that the code might not work with an older version of the component. So always make sure you are using the latest version of the component.
Download the example code
Download creatingxmlwithopenxml.zip (253 kB) which contains the sample application demonstrated in this article. Please note that you must also download and install the latest OpenXML component (version 2.3.3+).
* * *
Need to use XML in your own applications? Let us help!