Custom Search SOABYTE here

Thursday, March 8, 2012

Converting multiple XML files to CSV files

IHAC which came up with a requirement that required all DVM files be changed to CSV format for their internal documentation. Although, it was a surprising requirement but indeed it had to be done.

This is how we can do it:--

1>       Change file extension of *.dvm to *.xml

Use MS-DOS to change rename all the files in the current directory that end with .dvm to .xml.

ren *.dvm *.xml

2>     Java program to change multiple .xml files to .csv files

package client;
import org.apache.xalan.xslt.Process;
public class xmltocsv {
           static int count=0;
   public static void main(String[] args) {
       String basedir="C:\\Documents and Settings\\TeamFusion\\Desktop\\dvm\\";
       String outdir="C:\\Documents and Settings\\TeamFusion\\Desktop\\csv\\";
       String input=null;
       String output=null;
       File file=new File("C:\\Documents and Settings\\TeamFusion\\Desktop\\dvm");
       String[] files=file.list();
       for(String filename: files) {
           output=outdir+filename.replaceFirst("xml", "csv");
       System.out.println("Total number of files converted to CSV  " +String.valueOf(count));
public static void convert(String input,String output)
       String[] abc={"-in",input,
                     "-xsl","C:\\Documents and Settings\\TeamFusion\\Desktop\\testdvmcsv.xsl",
       catch(Exception e) {
           System.out.println("Conversion failed " + input);

To run this java program one need to do following pre-requisites:--

1>    Testdvmcsv.xsl  needs to be placed in file-path accordingly.

<xsl:stylesheet version="1.0" xmlns:xsl="" xmlns:ns="">
  <xsl:output method="text" encoding="utf-8" />

  <xsl:param name="delim" select="','" />
  <xsl:param name="quote" select="'&quot;'" />
  <xsl:param name="break" select="'&#xA;'" />

  <xsl:template match="/">
  <xsl:apply-templates select="ns:dvm/ns:columns" />
    <xsl:apply-templates select="ns:dvm/ns:rows/ns:row" />
   <xsl:template match="ns:columns">
           <xsl:for-each select="ns:column">
                <xsl:value-of select="@name"/>
                <xsl:if test="position() != last()">
                    <xsl:value-of select="$delim"/>
                        <xsl:value-of select="$break" />

  <xsl:template match="ns:row">
    <xsl:apply-templates />
    <xsl:if test="following-sibling::*">
      <xsl:value-of select="$break" />

  <xsl:template match="*">
    <!-- remove normalize-space() if you want keep white-space at it is -->
    <xsl:value-of select="concat($quote, normalize-space(), $quote)" />
    <xsl:if test="following-sibling::*">
      <xsl:value-of select="$delim" />

  <xsl:template match="text()" />

2       2>   You need to include the full path to .jar files in your CLASSPATH environment variable.
·    xalan.jar
·    xml-apis.jar
·    xercesImpl.jar
·    xalan27.jar
Download these jars from:--


To update your CLASSPATH on Linux, put these lines in your .profile file:


On Windows, use the Control Panel to open the System icon, where you can set environment variables for Windows. Use semicolons instead of colons to separate items in the CLASSPATH.

You can tell the Xalan processor is working by running this command from cmd:

java  org.apache.xalan.xslt.EnvironmentCheck

It reports on the Java environment and the version of Xalan. If you get a message about a class not found, then your CLASSPATH is not set up right to use Xalan.


Post a Comment

Blogger Profile