/* 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 an XML file into a CSV file. * In this example, all the data from the XML file is stored * in 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: StreamingXml2Csv.java * For this example, the source file is GetSearchResults.xml. This * file was exported using the eBay web services API, and represents * the actual XML results you can expect from eBay. The data are from * the eBay sandbox and are for test purposes. We have deliberately * used an external XML file to demonstrate the flexibility of the CSV and * XML Manager components. */ public class Xml2Csv { // public methods public static void main( String[] args ) { File xml = new File("GetSearchResults.xml"); File csv = new File("GetSearchResults.csv"); convert( xml, csv ); } public static void convert( File pXmlFile, File pCsvFile ) { // Create the XML Manager that we will use, and // define the eBay namespace so that we use it in our // XPath expressions. XmlManager xmlman = new XmlManager(); XmlSpec xmlspec = xmlman.getXmlSpec(); xmlspec.addNamespace("e","urn:ebay:apis:eBLBaseComponents"); // Also create the CSV Manager. CsvManager csvman = new CsvManager(); CsvSpec csvspec = csvman.getCsvSpec(); csvspec.setEncoding("UTF-8"); // These XPath expressions define the data fields for the CSV file, // in the order that they appear. They are relative to the record // element: SearchResultItem. String[] fieldpaths = new String[] { "e:Item/e:ItemID", "e:Item/e:ListingDetails/e:StartTime", "e:Item/e:ListingDetails/e:EndTime", "e:Item/e:ListingDetails/e:ViewItemURL", "e:Item/e:SellingStatus/e:BidCount", "e:Item/e:SellingStatus/e:CurrentPrice/@currencyID", "e:Item/e:SellingStatus/e:CurrentPrice", "e:Item/e:Site", "e:Item/e:Title", "e:Item/e:Country", }; // The RecordSpec specifies the main data record element: SearchResultItem RecordSpec itemspec = new RecordSpec("/e:GetSearchResultsResponse/e:SearchResultItemArray/e:SearchResultItem", fieldpaths ); // Load all the XML data records. List xmldata = xmlman.load( pXmlFile, itemspec ); // Create a new record list for the CSV output, so that we can set the CSV file headers. ArrayList csvdata = new ArrayList(); csvdata.add( new String[] {"ItemID","StartTime","EndTime","ViewItemURL","BidCount","Currency","Price","Site","Title","Country"} ); csvdata.addAll( xmldata ); // Save the data records as CSV. csvman.save( pCsvFile, csvdata ); } }