Hi,

As the initial step, we had to read data from JSON and CSV using selectors.
Selector format was decided to be the following.

> employee.firstname
> customers.customer.account.number


Following libraries were used to read data by converting the selector to
jsonpath and CSV header.

*Axiom* to read XML

*Jayway jsonpath*

elementPath = "$.." + elementPath;

nodesList = new ArrayList<Object>();

 nodesList = JsonPath.read(jsonString, elementPath); //elementPath-jsonpath

*Javacsv 2.0*

csvReader = new CsvReader(inputFile.getCanonicalPath());

csvReader.readHeaders();

inputValueList = new ArrayList<Object>();
while(csvReader.readRecord()){

inputValueList.add(csvReader.get(elementPath)); //elementPath-CSV header

 }

In an intermediate step, processed data is stored in a hashmap and
therefore I had to write data from the hashmap to different data types
(XML, JSON, CSV). Following is what I have tried.

*DOM* as a solution to create output XML.

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();

 DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
 doc = docBuilder.newDocument();

Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
int i=0;
while (iterator.hasNext()) {

Map.Entry<String,String> pairs = (Map.Entry<String,String>)iterator.next();
//Set root node
if(i==0){

Element rootElement =
doc.createElement(pairs.getKey().toString().split("\\.")[0]);
doc.appendChild(rootElement);
mappedNodes.put(pairs.getKey().toString().split("\\.")[0], rootElement);

}
chooseNode(pairs.getKey().toString(), pairs.getValue().toString());
i++;

}

TransformerFactory transformerFactory = TransformerFactory.newInstance();

 Transformer transformer = transformerFactory.newTransformer();
 DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(file);
transformer.transform(source, result);

*Google Gson* as a JSON builder.

baseNode = new HashMap<String, Object>();

List<Map<String, Object>> firstNode = new ArrayList<Map<String, Object>>();

baseNode.put(key.substring(0, key.indexOf(".")), firstNode); //first node

if(getNode instanceof List)

((List<List<Map<String, Object>>>) getNode).add(list);

else if(getNode instanceof Map)

((Map<String, Object>) getNode).put(lastNode,
(value.equals(""))?list:value);

Gson gson = new GsonBuilder().setPrettyPrinting().create();

String jsonString = gson.toJson(baseNode);

*Open-csv *CsvWriter to write CSV with header.

 writer = new CSVWriter(new FileWriter(file));
Set<String> keySet = map.keySet();
List<String> keyList = new ArrayList<String>();
keyList.addAll(keySet);
//Csv header
List<String> csvHeader = new ArrayList<String>();
Iterator<String> it = keySet.iterator();
while ( it.hasNext() ){

 csvHeader.add(it.next());

 }
writer.writeNext(csvHeader.toArray(new String[csvHeader.size()]));
//Csv data
List<String[]> csvData = new ArrayList<String[]>();
for(int i=0; i < map.get( keySet.iterator().next() ).size() ; i++){

String[] stringArray = new String[keyList.size()];
//iterate over keyList
for(int j=0; j<keyList.size(); j++){

 stringArray[j] = map.get(keyList.get(j)).get(i);

 }
csvData.add(stringArray);

}
writer.writeAll(csvData);
writer.close();

WDYT? Are there any better ways to do this?

-- 
*Gayan Kaushalya Yalpathwala*
 Software Engineer
WSO2 Inc.; http://wso2.com
lean.enterprise.middleware

mobile: +94 71 8682704
_______________________________________________
Dev mailing list
Dev@wso2.org
http://wso2.org/cgi-bin/mailman/listinfo/dev

Reply via email to