Avatar of enigmasolutions
enigmasolutions

asked on 

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
DelphiXML

Avatar of undefined
Last Comment
aikimark
Avatar of aikimark
aikimark
Flag of United States of America image

If you can use a query in place of a table, then you can rename the columns with SQL.
Avatar of enigmasolutions
enigmasolutions

ASKER

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.

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

ASKER CERTIFIED SOLUTION
Avatar of aikimark
aikimark
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of enigmasolutions

ASKER

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.
Avatar of aikimark
aikimark
Flag of United States of America image

@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.
Delphi
Delphi

Delphi is the most powerful Object Pascal IDE and component library for cross-platform Native App Development with flexible Cloud services and broad IoT connectivity. It provides powerful VCL controls for Windows 10 and enables FMX development for Windows, Mac and Mobile. Delphi is your choice for ultrafast Enterprise Strong Development™. Look for increased memory for large projects, extended multi-monitor support, improved Object Inspector and much more. Delphi is 5x faster for development and deployment across multiple desktop, mobile, cloud and database platforms including 32-bit and 64-bit Windows 10.

60K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo