Dataset to XML in Delphi

Posted on 2011-10-03
Medium Priority
Last Modified: 2012-05-12
Hi experts,

Using Delphi 2009, I'm trying to export the data from a delphi dataset to an XML file.  See attached for the desired XML output.  I'm after a generic solution here that can be used on other datasets in the future rather than something hard coded.

The catch is that the XML element names in the output file don't corresspond to dataset fieldnames...

So I was thinking something along the lines of a procedure where you pass in the dataset + something mapping dataset fieldnames to XML elements/attributes.  See attached for what I had in mind for fieldname mapping - I've basically replaced the XML data contents with fieldnames.  This is just an idea - I'm open to suggestions.  This procedure would return the XML file (as per attached).

Does something like this already exist in Delphi?  Sample code would be a great help.

Thanks in advance, all comments will be appreciated.
Question by:enigmasolutions
  • 3
  • 2
LVL 46

Expert Comment

ID: 36907474
If you can use a query in place of a table, then you can rename the columns with SQL.

Author Comment

ID: 36907569
Hi aikimark, thanks for the comment, it's a good idea but unfortunately the fieldnames are fixed (the dataset is used for other things besides this which relies on fixed fieldnames so can't change the query).  Even if that wasn't the case one of the fields (called GSTin) becomes an attribute, while the others become elements in the XML file so we need to work out some way of determining this too.
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 36914229

If you want a real generic solution then the simplest way would be to use TClientDataset which has a method for saving to XML.

Otherwise you have to answer questions such as
What determines whether a field contains an attribute or whether its a child node?
What happens when you get a dataset that has totally different fields?

You could create a class that does the conversion but I suppose you will have to create properties to define how the nodes are created,
Have a FieldList to store the fields that can be streamed to XML
The individual field could have a property such as parent field, or isattribute

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 46

Accepted Solution

aikimark earned 2000 total points
ID: 36914374
Have you looked at some of the third party vendor products, such as SMExport?
TSMExportToXML: http://www.scalabium.com/sme/

Although you posted XML files (thanks), they don't provide any mapping information.  Neither of them has an attribute, which you mentioned in your question text.

You might also consider post processing the XML output from your Delphi program, using XSLT to get the XML into the form/schema you want.

Author Closing Comment

ID: 36914606
Great thanks that was what I was after, an XSLT transform afterwards will work well - wasn't aware that Delphi could do this but can using IXMLNode.TransformNode.
LVL 46

Expert Comment

ID: 36916038

Glad I could help.  Thanks for the points.

Once IXMLNode.TransformNode is working, you might consider writing an EE article on this.  All you'd need to show is how to rename fields and how to make an element an attribute.  

Bonus content (or a second article) might be record/element filtering or XHTML transformation.

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question