Using Delphi XML Mapper
Embarcadero Enterprise Studio package contains a wonderful utility that can assist any Delphi developer with the ability of importing a XML document for the sole purpose of using it within a datagrid. Displayed below is a simple SSH cheat XML document that was created using Excel and will be used.
The purpose of this entire exercise to get the XML data into a clientdataset so it can be used in a datagrid.
XML Mapper is a stand-alone tool located in the bin folder or you can click on tools and you will see the item called “XML Mapper”. The tool allows any type of XML document file to be converted over to a data packet file (*.xml,*.cds), schema files (*.dtd,*.xdr,*.xsd), repository files (*.xrp) and transformation files (*.xtr), or vice versa where you can load a datapacket back into an XML file.
Once the file is loaded, you have the ability of viewing either the document or schema view by clicking the tabs below on the left side. The tabs on top are DTD, XDR or XML-Schema as displayed below.
Create and test transformation
- First select the radio button that indicates what the transformation creates:
- Choose the Datapacket to XML button if the mapping goes from data packet to XML document.
- Choose the XML to Datapacket button if the mapping goes from XML document to data packet.
- If you are generating a data packet, you will also want to use the radio buttons in the Create Datapacket As section. These buttons let you specify how the data packet will be used: as a dataset, as a delta packet for applying updates, or as the parameters to supply to a provider before fetching data.
- Click Create and Test Transformation to generate an in-memory version of the transformation. XML mapper displays the XML document that would be generated for the data packet in the Datapacket pane or the data packet that would be generated for the XML document in the XML Document pane.
- Finally, choose File > Save > Transformation to save the transformation file. The transformation file is a special XML file (with the .xtr extension) that describes the transformation you have defined.
The image below shows the mapping with the field types and the lengths.
Upon clicking on the button the result is displayed with the XML data mapped to the particular field. So now i can save this as a datapacket file and have my clientdataset read the information.
Here is a simple application that will display the XML on a datagrid.
So just follow these simple steps to create an XTR file from XML in Delphi using XML Mapper:
1. Open the XML Mapper Tools from RAD Studio.
2. Load the XML document. All the nodes will appear on the left section of XML Mapper.
3. Right click on left section and select “Select All Children” option or simply click on each field.
4. Hit “Create and Test Transformation” button in the middle section of XML Mapper.
5. You can than save the Datapacket to a XTR file for use in your application. It is that simple.
Click on the logo for more information