Using XMLMapper and XML Transforms with Kylix 2 Enterprise

By: John Kaster

Abstract: This article describes the new Borland technology for transforming XML documents into dataset representations for easy modification

This information is an excerpt from a chapter on the CD of the book "Kylix Developer's Guide." You can find more information about the book on Amazon, or our publisher, SAMS.

XML Transforms

XML Transforms provide a functionality unique to Borland. With the XMLMapper utility, you can map an XML document directly to a ClientDataSet, and process it as though it were a simple dataset. When you are done, you can save the changes back out to the XML document.

To demonstrate, we'll use the same purchase order XML document we just used. Run the XMLMapper utility by invoking it from your desktop menu in the Kylix 2 menu group. Load the PurchaseOrder.xml document. Right mouse click in the tree view showing the structure of the document you just loaded and choose Select All. You will see a list of the selected nodes in the "mapper" table in the center of the application. Then select Create|Datapacket from XML to map those nodes into a ClientDataSet structure.


Click the Create and Test Transformation button to see how the XML document will be mapped into a ClientDataSet that can be edited with a standard grid.

Test grid

Be sure to click on the item dataset and open it up, to see that multiple items are supported in the dataset.

Detail items

Transforming XML to a Datapacket

Now that you've verified that the transformation works, you can create the component-level support you need to use this in your own Kylix applications. Select File|Save|Transformation and save the file as its default name, ToDp.xtr. (If this is not the default name of the file, you have not set up the transformation correctly, and you may want to refer to the help for XMLMapper which provides more details.)

Transforming a DataPacket to XML

To create the transformation that puts data from the datapacket back into an XML document, set the transform direction radio group to "Datapacket to XML" on the Mapping tab then create and test the transformation and =select File|Save|Transformation to save the file as its default name, ToXML.xtr.

Using the transforms

Now that we have the transformations for both files, we can create an application that will load the XML file into a ClientDataSet and allow you to use DataCLX components to manipulate it, then save it back to an XML document. Create a new application with File|New Application and do the following:

  1. Drop an XMLTransformProvider from the component palette's Data Access tab onto the form, set its TransformRead.TransformationFile property to the file ToDp.xtr, its TransformWrite.TransformationFile to ToXML.xtr, and its XMLDataFile to "PurchaseOrder.xml".
  2. Drop a ClientDataSet on the form and set its ProviderName to "XMLTransformProvider1"
  3. Drop a DataSource on the form and set its DataSet to ClientDataSet1
  4. Drop a DBNavigator and DBGrid on the form and set their DataSource property to DataSource1
  5. Set ClientDataSet1.Active and you should see the XML data from the purchase order in the grid.
  6. Drop a Button on the form, set its Caption to "&Apply".
  7. Double-click on the button and set its event code to:

You can now run the application, make changes to the data in the XML document, and apply those changes by clicking the Apply button. That's all there is to it. Of course, there are ways to customize XML transforms, but that will have to wait for a second edition of the book.

Or, in this case, another article on the community site. I hope you find this quick tutorial helpful.

Server Response from: ETNASC03