XML to CSV: Detail Design


Main Program

Again, we put the main processing logic in a routine that is called from a shell main program. The shell main program functions are described next .

Logic for the Shell Main Routine for XML to CSV
 Arguments:   Input Directory Name   Output CSV File Name   File Description Document Name Options:   Validate input   Help Validate and process command line arguments IF help option specified   Display help message   Exit ENDIF Open output file Create new CSVTargetConverter object, passing:     Output Stream     File Description Document Name Set up implementation dependent DOM environment for loading,     parsing, and validating input documents Open input directory Get first file from input directory DO for all files in input directory   Input Document <- Load input file, validating it if requested   Call CSVTargetConverter processDocument method, passing       the Input Document   Increment number of documents processed ENDDO Close output file Display completion message with number of documents processed 

CSVTargetConverter Class (Extends TargetConverter)

Overview

The CSVTargetConverter is the main driver for the conversion. It is derived from the TargetConverter base class (see Chapter 6). It takes an input XML Document as a DOM Document and writes it to the CSV file output stream. This structuring facilitates using the converter in future situations in which the input might come from an XSLT transformation in memory, producing a DOM Document as the result, rather than just a disk file. Here are its attributes and methods .

Attributes:

  • None

Methods:

  • Constructor

  • processDocument

Methods
Constructor

Similar to the source converter, the constructor method for the CSVTarget Converter object sets up that object and the CSVRecordWriter object.

Logic for the CSVTargetConverter Constructor Method
 Arguments:   Output Stream CSV Output file   File Description Document Name Call base class constructor, passing File     Description Document Name Create CSVRecordWriter object, passing:     File Description DOM Document and Output Stream 
processDocument

The bulk of the processing is performed in the CSVTargetConverter's processDocument method. This method converts one input XML Document and writes it to the CSV output stream based on the input parameters. The logic is much simpler than the CSVSourceConverter's processFile method since we don't have to test for document or partner breaks or deal with saving DOM documents.

Logic for the CSVTargetConverter processDocument Method
 Arguments:   DOM Document Input Document Returns:   Error status or throws exception Row Grammar <- Get "RowDescription" Element from Grammar Element Grammar Row Element Name <- get Row Grammar Element's     "ElementName" Attribute Root Element <- Get Document's documentElement attribute Root Element Name <- Get Root Element's tagName attribute IF Root Element Name != Grammar Root Element Name   Return error ENDIF DO for child Elements of Root Element   Row Element Name <- Get Row Element's tagName attribute   IF (Row Element Name != Grammar Row Element Name)     Return error   ENDIF   Call CSVRecordWriter's parseRecord method to load DataCell       Array from Elements for Row and Columns, passing       Record Element and Row Grammar   Call CSVRecordWriter's writeRecord to write output record       from DataCell Array ENDDO Return success 

CSVRecordWriter Class (Extends RecordWriter)

Overview

The CSVRecordWriter class is derived from the RecordWriter base class (see Chapter 6). We have only a few extensions to that class. We use the parseRecord method inherited from the RecordWriter base class.

Attributes:

  • Character or Byte Column Delimiter

  • Character or Byte Text Delimiter

Methods:

  • Constructor

  • writeRecord

Methods
Constructor

Here is the logic for the CSVRecordWriter constructor method.

Logic for the CSVRecordWriter Constructor Method
 Arguments:   DOM Document File Description Document   Output Stream Call RecordWriter base class constructor, passing File     Description Document and Output Stream Record Terminator <- Get "RecordTerminator" Element's "value"     Attribute from File Description Document Call setTerminator to set the Record Terminators Column Delimiter <- Get "ColumnDelimiter" Element's "value"     Attribute from File Description Document Text Delimiter <- Get "TextDelimiter" Element's "value"     Attribute from File Description Document 
writeRecord

The writeRecord method is unique to each RecordWriter derived class. This method in the CSVRecordWriter class takes the content of the DataCell Array, formats it to the CSV data types, builds a string for the output record, and finally writes it to the output stream.

Logic for the CSVRecordWriter writeRecord Method
 Arguments:   None Returns:   Error status or throws exception Initialize Output Record Buffer Output Column Number <- 1 DO for Index from 0 through Highest Cell   Cell Column Number = Get Cell's Field Number   DO while Output Column Number < Cell Column Number     Append Column Delimiter to Output Record Buffer     Increment Output Column Number   ENDDO   Call fromXML to convert Cell contents from XML datatype   Delimit Text Flag <- call Cell's delimitText method   Cell Contents <- Call getField to retrieve cell buffer       contents   Clear Array entry   IF Delimit Text Flag is true     Append Text Delimiter character to Output Record Buffer     Append Cell Contents to Output Record Buffer     Append Text Delimiter character to Output Record Buffer   ELSE     Append Cell Contents to Output Record Buffer   ENDIF ENDDO Highest Cell <- -1 Append base RecordHandler's Record Terminators to Output Record     Buffer Call language's write routines to do physical write of     Output Record Buffer Return success 


Using XML with Legacy Business Applications
Using XML with Legacy Business Applications
ISBN: 0321154940
EAN: 2147483647
Year: 2003
Pages: 181

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net