Not every application offers the ability to import data by means other than a user keying it. However, nearly every application has the ability to create printed documents, even if it doesn't provide features for exporting data files. Certain steps always have to be performed. Certain aspects aren't that much different for printing, exporting flat files, or exporting XML documents.
The key thing to bear in mind when coding your XML export as opposed to a flat file export is that we generally export several logical documents to one physical flat file. For XML we must export each logical document to a separate physical file. A well-formed XML document can have only a single top-level Element, the document root Element. Writing two documents to the same file violates this constraint.
We can still model our XML export on an existing extract or print routine. The example below shows typical approaches at a high level. In this simplified example of an invoice export, we'll assume that we're extracting data from two relational database tables, one for the overall invoice information and the other for item details. We're going to write the invoices to disk files, though they might be handed off to communications modules as DOM Document objects. Here's the logic for a typical flat file extract routine.
Arguments: Selection criteria, e.g., invoice numbers Output file name Open output file Build and open cursor for selecting invoice headers Fetch first invoice header row DO for each invoice header Format data for export Write record to output file Open secondary cursor for line item details Fetch first line item detail row DO For each line item detail row Format data for export Write record to output file ENDDO Close secondary cursor ENDDO Close main cursor Close output file Return |
The XML export routine follows the same general logic for extracting the data from the database but differs in some key areas. We'll again assume a DOM API. Other models for processing XML will differ in a few details, but the overall flow will be identical.
Arguments: Selection criteria, e.g., invoice numbers Output directory specification Set up API for creating XML document Build and open cursor for selecting invoice headers Fetch first invoice header row DO for each invoice header Create new XML document Create root Element and attach to document Build output filename Create invoice header element and attach to root DO for each column to be added to document Create Element and attach to invoice header Create Text node and attach to column Element ENDDO Open secondary cursor for line item details Fetch first line item detail row DO for each line item detail row Create line item Element and attach to root Element DO for each column in line item row to be added to document Create Element and attach to line item Element Create Text node and attach to column Element ENDDO Close secondary cursor ENDDO Save DOM document to file ENDDO Close main cursor Return |
There is an important difference, aside from the obvious details of formatting and writing a flat file record as opposed to creating Elements. Rather than opening the output file at the beginning of the routine and closing it at the end, for each iteration of the DO loop that processes an invoice header row we create and save a new XML document.
Of course, every application will be somewhat different, but this example illustrates some of the major issues to consider.