CTTL on
SourceForge |
Download
Latest |
Documentation
Index |
Library
News |
CVS
Repository |
Other
Links |
This utility converts input file in comma separated value (CSV) format into its XML equivalent, assuming the first line contains the column headers. Usage:
>csv2xml file.csv
Output is sent to standard output.
// Converts input file in comma separated value (CSV) format into // its XML equivalent, assuming the first line contains column headers. // Usage: >csv2xml file.csv // Output is sent to standard output. // Must appear before assert.h to stop assertions from being compiled: //#define NDEBUG #include "cttl/cttl.h" #include "utils/fileio.h" using namespace cttl; int main(int argc, char* argv[]) { assert( argc > 1 ); input<> inp; file2string( argv[ 1 ], inp.text() ); assert( inp.length() ); const_edge<> universe( new_edge( inp ) ); const_edge<> line( new_edge( inp ) ); const_edge<> cell( new_edge( inp ) ); std::vector< std::string > vect_columns; while ( universe.length() && quote( true, line( entity() ), '\n' | end() ).match( universe ) != std::string::npos ) { int line_count = line.first.line(); int column_count = 0; if ( line_count != 1 ) std::cout << "<line count=" << line_count << '>' << std::endl ; while ( line.length() && (( ( ansi_double_quote( cell( entity() ) ) + ( ',' | end() ) ) | ( cell.first( begin( true ) ) + !cell.second( ',' | end() ) ) ).match( line ) != std::string::npos ) ) { if ( line_count == 1 ) { vect_columns.push_back( cell.text() ); } else { std::cout << '\t' << '<' << vect_columns[ column_count ] << '>' << cell.text() << "</" << vect_columns[ column_count ] << '>' << std::endl ; } ++column_count; } if ( line_count != 1 ) std::cout << "</line>" << std::endl ; } assert( !universe.length() ); return 0; }
Permission to copy, use, modify, sell and distribute this document is granted provided this copyright notice appears in all copies. This document is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose.