/* Copyright (c) 2005-2006 Ricebridge. All Rights Reserved. * * This file is available under the terms and conditions of the * Ricebridge "Open Source API" policy; Ricebridge grants use of this * copyrighted work under the terms of a BSD-style license only. See * http://www.opensource.org/licenses/bsd-license.php for more * information. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution. * * - Neither the name of the Ricebridge nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. */ import com.ricebridge.xmlman.XmlManager; import com.ricebridge.xmlman.RecordSpec; import com.ricebridge.xmlman.XmlSpec; import com.ricebridge.csvman.CsvManager; import com.ricebridge.csvman.CsvSpec; import java.util.*; import java.io.*; /** Convert a CSV file into an XML file. * In this example, the entire CSV file is loaded into memory. * This is the simplest case, and it will work so long as you have enough * memory to hold all the data. If you are dealing with multi-gigabyte files, * it is better to use the streaming solution: StreamingCsv2Xml.java * For this example, the source file is report.csv. This file was exported * from the Google Adwords report system, so it is the actual data format used * by Google (the values have been changed, however). We have deliberately * used an external CSV file to demonstrate the flexibility of the CSV and * XML Manager components. */ public class Csv2Xml { // public methods public static void main( String[] args ) { File csv = new File("report.csv"); File xml = new File("report.xml"); convert( csv, xml ); } public static void convert( File pCsvFile, File pXmlFile ) { // Create the CsvManager that we will use. // Actual data only starts on the 8th line. CsvManager csvman = new CsvManager(); CsvSpec csvspec = csvman.getCsvSpec(); csvspec.setEncoding("UTF-8"); csvspec.setStartLine(8); // Also create the XmlManager, and // set the standard XML header (this is optional). XmlManager xmlman = new XmlManager(); XmlSpec xmlspec = xmlman.getXmlSpec(); xmlspec.setHeader( "\n" ); // Specify the format of the output XML. // Notice that the first three fields are absolute. The data records // will be grouped by these fields, as parent XML elements. String[] fieldpaths = new String[] { "/report/campaign/day/@date", "/report/campaign/@name", "/report/campaign/@group", "@text", "@match", "@status", "cpc/@min", "cpc/@max", "cpm/@max", "url", "clicks/@imp", "clicks/@count", "clicks/@ctr", "clicks/@avgcpc", "clicks/@avgcpm", "cost", "avgpos", }; // The RecordSpec specifies the main record element: keyword RecordSpec keyspec = new RecordSpec("/report/campaign/day/keyword", fieldpaths ); // Load all the CSV data. List csvdata = csvman.load( pCsvFile ); // Last line is totals, so remove it. csvdata.remove( csvdata.size()-1 ); // Save the data records as XML. xmlman.save( pXmlFile, keyspec, csvdata ); } }