Dataset to XML in Delphi

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.
Who is Participating?
Have you looked at some of the third party vendor products, such as SMExport?

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.
If you can use a query in place of a table, then you can rename the columns with SQL.
enigmasolutionsAuthor Commented:
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.
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Ephraim WangoyaCommented:

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

enigmasolutionsAuthor Commented:
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.

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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.