I'm at the end of my capabilities with what I'm trying to achieve here, I need to create a converter using groovy to convert CSV to XML. My ability is very limited groovy, so the following code may be a bunch of garbage, but it was put together with other pieces of code that I found.
package com.ns.extension.tee;
import com.ns.argo.business.api.ArgoUtils
import com.ns.argo.business.api.GroovyApi
import org.apache.log4j.Logger
import org.jdom.Document
import org.jdom.Element
import javax.xml.parsers.DocumentBuilder
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.transform.Result
import javax.xml.transform.Source
import javax.xml.transform.Transformer
import javax.xml.transform.TransformerFactory
import javax.xml.transform.dom.DOMSource
import javax.xml.transform.stream.StreamResult
public class CSVtoXML extends GroovyApi {
public void execute(Map param) {
LOGGER.warn(String.format("At start of %s at %s", this.getClass().getName(), ArgoUtils.timeNow()));
convertCSVToXML();
LOGGER.warn(String.format("At end of %s at %s", this.getClass().getName(), ArgoUtils.timeNow()));
}
private void convertCSVToXML() {
List<String> headers = new ArrayList<String>(5);
String path = "C:\\convert\\";
path = path.replaceAll("\\", "/");
File file = new File(path, "CRR.csv");
BufferedReader reader = null;
try {
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
Document newDoc = domBuilder.newDocument() as Document;
Element rootElement = newDoc.createElement("XMLCreators");
newDoc.appendChild(rootElement);
reader = new BufferedReader(new FileReader(file));
int line = 0;
String text = null;
while ((text = reader.readLine()) != null) {
StringTokenizer st = new StringTokenizer(text, " ", false);
String[] rowValues = new String[st.countTokens()];
int index = 0;
while (st.hasMoreTokens()) {
String next = st.nextToken();
rowValues[index++] = next;
}
if (line == 0) {
for (String col : rowValues) {
headers.add(col);
}
} else {
Element rowElement = newDoc.createElement("row");
rootElement.appendChild(rowElement);
for (int col = 0; col < headers.size(); col++) {
String header = headers.get(col);
String value = null;
if (col < rowValues.length) {
value = rowValues[col];
} else {
value = "";
}
Element curElement = newDoc.createElement(header);
curElement.appendChild(newDoc.createTextNode(value));
rowElement.appendChild(curElement);
}
}
line++;
}
ByteArrayOutputStream baos = null;
OutputStreamWriter osw = null;
try {
baos = new ByteArrayOutputStream();
osw = new OutputStreamWriter(baos);
TransformerFactory tranFactory = TransformerFactory.newInstance();
Transformer aTransformer = tranFactory.newTransformer();
aTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
aTransformer.setOutputProperty(OutputKeys.METHOD, "xml");
aTransformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
Source src = new DOMSource(newDoc);
Result result = new StreamResult(osw);
aTransformer.transform(src, result);
osw.flush();
System.out.println(new String(baos.toByteArray()));
} catch (Exception exp) {
exp.printStackTrace();
} finally {
try {
osw.close();
} catch (Exception e) {
}
try {
baos.close();
} catch (Exception e) {
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private final Logger LOGGER = Logger.getLogger(CSVtoXML.class);
}
I would like it to be displayed in the following format, all with different meanings, this is just an example.
<?xml version="1.0" encoding="UTF-8"?>
<XMLCreators>
<row>
<Name>chi</Name>
<Age>23</Age>
<sex></sex>
</row>
<row>
<Name>kay</Name>
<Age>19</Age>
<sex>male</sex>
</row>
<row>
<Name>john</Name>
<Age></Age>
<sex>male</sex>
</row>
</XMLCreators>
CSV example:
Job Type Cntr Number Date Booking Ref Commodity Weight Special Instructions
JOB0001 Circle 12 31/09/2013 Book0001 1 100.00 Carry
JOB0002 Square 13 31/11/2013 Book0001 2 200.00 None
JOB0003 Cube 15 31-Dec-13 Book0001 3 300.00 Hide
-------------------------------------------- ------ ------------------------------------------
02/03/2014 Current Code
package com.ns.extension.tee;
import com.ns.argo.business.api.ArgoUtils
import com.ns.argo.business.api.GroovyApi
import static com.xlson.groovycsv.CsvParser.parseCsv
import groovy.xml.*
import org.apache.xalan.*
public class CSVtoXML extends GroovyApi {
public void execute(Map param) {
new File( 'C:/convert/CRR_output.xml' ).withWriter { w ->
new File( 'C:/convert/CRR.csv' ).withReader { r ->
def csvParser = parseCsv( r , speerator: ',')
w.println new StreamingMarkupBuilder().bind {
XMLCreators {
csvParser.each { line ->
println line
if( line ['Job'] ){
row {
job( line[ 'Job' ] )
type( line[ 'Type' ] )
cntr( line[ 'Cntr Number' ] )
date( line[ 'Date' ] )
bref( line[ 'Booking Ref' ] )
comm( line[ 'Commodity' ] )
weig( line[ 'Weight' ] )
spci( line[ 'Special Instructions' ] )
}
}}
}
}
}
}
}
static main( args ) {
new CSVtoXML().execute( [:] )
}
}
source
share