[Dev] CSV -> XML transformation using XSLT mediator

2014-12-01 Thread Lasindu Charith
Hi all,

Does anybody know a way(sample) to transform CSV format to XML in ESB,
using some mediator except Smooks? (such as XSLT)

Thanks,
Lasindu

-- 
*Lasindu Charith*
Software Engineer, WSO2 Inc.
Mobile: +94714427192
Web: blog.lasindu.com
___
Dev mailing list
Dev@wso2.org
http://wso2.org/cgi-bin/mailman/listinfo/dev


Re: [Dev] CSV -> XML transformation using XSLT mediator

2014-12-01 Thread Senduran Balasubramaniyam
Hi Lasindu,

Could you please let us know a sample input format and desired output
format

Thanks
Senduran

On Mon, Dec 1, 2014 at 2:11 PM, Lasindu Charith  wrote:

> Hi all,
>
> Does anybody know a way(sample) to transform CSV format to XML in ESB,
> using some mediator except Smooks? (such as XSLT)
>
> Thanks,
> Lasindu
>
> --
> *Lasindu Charith*
> Software Engineer, WSO2 Inc.
> Mobile: +94714427192
> Web: blog.lasindu.com
>
> ___
> Dev mailing list
> Dev@wso2.org
> http://wso2.org/cgi-bin/mailman/listinfo/dev
>
>


-- 
*Senduran *
Software Engineer,
WSO2, Inc.;  http://wso2.com/ 
Mobile: +94 77 952 6548
___
Dev mailing list
Dev@wso2.org
http://wso2.org/cgi-bin/mailman/listinfo/dev


Re: [Dev] CSV -> XML transformation using XSLT mediator

2014-12-01 Thread Lasindu Charith
Hi Senduran,

It should be comma separated csv to XML format.

*FROM: CSV*
abc, 123, 0.1, xyz,
pqr, 456, 0.01, qwe,
zxy, 789, 0.45, lmn

*TO: XML*

  

abc

123

0.1
xyz

   


pqr

456

0.01
qwe

   


zxy

789

0.45
lmn

   




On Mon, Dec 1, 2014 at 6:20 PM, Senduran Balasubramaniyam  wrote:

> Hi Lasindu,
>
> Could you please let us know a sample input format and desired output
> format
>
> Thanks
> Senduran
>
> On Mon, Dec 1, 2014 at 2:11 PM, Lasindu Charith  wrote:
>
>> Hi all,
>>
>> Does anybody know a way(sample) to transform CSV format to XML in ESB,
>> using some mediator except Smooks? (such as XSLT)
>>
>> Thanks,
>> Lasindu
>>
>> --
>> *Lasindu Charith*
>> Software Engineer, WSO2 Inc.
>> Mobile: +94714427192
>> Web: blog.lasindu.com
>>
>> ___
>> Dev mailing list
>> Dev@wso2.org
>> http://wso2.org/cgi-bin/mailman/listinfo/dev
>>
>>
>
>
> --
> *Senduran *
> Software Engineer,
> WSO2, Inc.;  http://wso2.com/ 
> Mobile: +94 77 952 6548
>



-- 
*Lasindu Charith*
Software Engineer, WSO2 Inc.
Mobile: +94714427192
Web: blog.lasindu.com
___
Dev mailing list
Dev@wso2.org
http://wso2.org/cgi-bin/mailman/listinfo/dev


Re: [Dev] CSV -> XML transformation using XSLT mediator

2014-12-01 Thread Tharindu Edirisinghe
Hi Lasindu,

I wrote a sample that does what you expect. Please check the attached.
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class Main {

public static void main(String[] args) {
List headers = new ArrayList();

File file = new File("/home/tharindu/Desktop/lasindu/abc.csv");
BufferedReader reader = null;

try {

DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder domBuilder = domFactory.newDocumentBuilder();

Document newDoc = domBuilder.newDocument();
// Root element
Element rootElement = newDoc.createElement("root");
newDoc.appendChild(rootElement);

reader = new BufferedReader(new FileReader(file));
int line = 0;

String text = null;

headers.add("val1");
headers.add("val2");
headers.add(("val3"));
headers.add("val4");

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.trim();

}

//String[] rowValues = text.split(",");


Element rowElement = newDoc.createElement("element");
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 {
// ?? Default value
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();
}
}
}

___
Dev mailing list
Dev@wso2.org
http://wso2.org/cgi-bin/mailman/listinfo/dev