Dataset to XML in Delphi

Posted on 2011-10-03
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
    LVL 44

    Expert Comment

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

    Author Comment

    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


    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

    LVL 44

    Accepted Solution

    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.
    LVL 1

    Author Closing Comment

    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 44

    Expert Comment


    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
    Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now