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.
XMLData.XML
XMLStructure.XML
LVL 1
enigmasolutionsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aikimarkCommented:
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.
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,
eg
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

C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

aikimarkCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
aikimarkCommented:
@enigmasolutions

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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.