Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I convert a component to XML? 1

Status
Not open for further replies.

Griffyn

Programmer
Jul 11, 2002
1,077
AU
I have a client/server app that generates a document (invoice, order, etc) in a TComponent descendent object which is then written into a .dfm style file (using TStream.WriteComponent, and ObjectBinaryToText), and finally opened in MS Excel and interpreted there for printing in a template.

Sample of the output file:
Code:
object TPurchase
  Branch = '273'
  TranNum = 'JX002'
  DateRaised = 40736
  DocumentName = 'Purchase Order'
  DocLabel = 'ORIGINAL'
  Details = <
    item
      OurStockCode = 'MISC'
      Description = ' Ladies V Neck Vest'
      OrderQty = 1
      Cost = 21
    end
    item
      OurStockCode = '/C'
      Description = '(Black) Size 10'
      OrderQty = 0
      IgnoreOnInternal = False
    end>
  Comment = 'DRIVER TO PICK UP'
  Revision = 0
end

This all works. The problem is that I had to write parsing routines in VBA for Excel to read the output file. This is really inefficient, as every property read must scan through every line before it. Some documents have many hundreds of lines, and it gets progressively slower and slower to read closer to the end of the file - on a modern machine some files take more than a minute to process.

Considering that XML is well known standard, I'd hoped I could use the XML capabilities of VBA to read my output if I could convert it to XML. And then, hopefully it would be a lot faster.

How do I do that?

Is there a better way I should be doing this? Perhaps I should create a proper excel worksheet in Delphi with unique identifiers and use Excel's VLOOKUP function?

Any thoughts?
 
I actually recently built a parser (not complete) especially for converting a DFM file to HTML.

I will have to dig up the code, I threw it aside a while back to start a new project. But you can tweak it to export the data as XML. It loads the DFM structure into an object hierarchy, with CSS properties etc. Let me know if you want it so I can piece it back together - it wandered off into a number of sub-projects on a similar subject and got mixed in the wave.

When a DFM file is loaded into this structure, it can then be converted down into any other language. You would have to replicate how it creates the HTML into XML instead, It's designed to convert the object structure into any other language (HTML being the only one it does currently).

The DFM to HTML conversion pretty much works. You can see the results below. The form I used to test with was one from an old dead project, which happened to have a lot of controls on it - perfect for testing.

HTMLView.png

HTMLText.png

DFMText.png

DFMObjects.png

ss-DelphiToHTML.png



JD Solutions
 
Actually I'd consider just taking the Delphi app straight to XML, unless Excel gives you something else functionally that your Delphi app isn't doing.

There are APIs to do this (as well as TXMLDocument, I think in the newer Delphis). I know for what I've been doing I've been playing a bit with MSXML. I don't know how djjd47130 approached it, but it looks like a great start.

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top