Quantcast

Converting CSV to XML

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Converting CSV to XML

ambarish.d
Hello,

I want to convert a CSV file to XML file in proper format. I have following code which is doing the Job but output is not a expected one.

 context.addRoutes(new RouteBuilder() {
                    public void configure() {
                        from("file://TEST?fileName=ArticleDistribution.csv").
                        unmarshal().
                        csv().
                        marshal().
                        xstream().
                        to("file://TESTOUT?fileName=ArticleDistribution.XML");
                    }
                });

For Example:
My CSV File looks like this:

Name, Age, Sex, Zip
Edward, 22,M,33639
Ema,27,F,30330

Output of Above code is:

<list>
  <java.util.Arrays_-ArrayList>
   
      <string>Name</string>
      <string>Age</string>
      <string>Sex</string>
      <string>Zip</string>
     

  </java.util.Arrays_-ArrayList>
<java.util.Arrays_-ArrayList>
   
      <string>Edward</string>
      <string> 22</string>
      <string>M</string>
      <string>33639</string>
     

  </java.util.Arrays_-ArrayList>
..
</List>

But Output I want is

<List>
<data>
<Name>Edward</Name>
<Age>22</Age>
<Sex>M</Sex>
<Zip>33639</Zip>
</data>
<data>
....
</data>
</List>

Can anybody please help me and show the proper way of doing this. It will be a great help.

thanks
Amby
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Converting CSV to XML

sekaijin
Hi.

Sorry, but if you understand french you can see
http://www.developpez.net/forums/d1216465/java/serveurs-conteneurs-java-ee/autres/camel-convertir-csv-xml-bindy-jaxb/

I use the target xsd schema to generate annotated classes.
I add the annotation Bindy
and voila

from("direct:start")
           .to("log: IN ==>")
           .unmarshal().bindy(BindyType.Csv, Poste.class.getPackage().getName())
           .bean(Transform.class)
           .marshal(jaxbDataFormat)
           .to("log: OUT ==>")
           .to("mock:result")
           ;
A + JYT

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Converting CSV to XML

ychawla
Hi Amby,
What about using a simple XSLT to go from the outputted format to your desired format?  It should be a pretty straight forward transformation.

Thanks,
Yogesh
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Converting CSV to XML

Christian Mueller
You cannot use XSLT to transform a CSV file into XML...


On Thu, May 17, 2012 at 10:19 PM, ychawla <[hidden email]>wrote:

> Hi Amby,
> What about using a simple XSLT to go from the outputted format to your
> desired format?  It should be a pretty straight forward transformation.
>
> Thanks,
> Yogesh
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/Converting-CSV-to-XML-tp5711086p5711598.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Converting CSV to XML

ychawla
Hi Christian,
You are correct that you can use XSLT on CSV, but he can get the data into this format:
<list>
  <java.util.Arrays_-ArrayList>
   
      <string>Name</string>
      <string>Age</string>
      <string>Sex</string>
      <string>Zip</string>
     
  </java.util.Arrays_-ArrayList>
<java.util.Arrays_-ArrayList>
   
      <string>Edward</string>
      <string> 22</string>
      <string>M</string>
      <string>33639</string>
     
  </java.util.Arrays_-ArrayList>
..
</List>

He can use an XSLT to go from that to this:

<List>
<data>
<Name>Edward</Name>
<Age>22</Age>
<Sex>M</Sex>
<Zip>33639</Zip>
</data>
<data>
....
</data>
</List>

Thanks,
Yogesh
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Converting CSV to XML

ramesri
In reply to this post by Christian Mueller
Yogesh, you can use camel flatpack component to parse the csv records.

And then use a Velocity template to output it to desired xml.

Thanks
Ramesh

On Thu, May 17, 2012 at 4:24 PM, Christian Mueller [via Camel] <[hidden email]> wrote:
You cannot use XSLT to transform a CSV file into XML...


On Thu, May 17, 2012 at 10:19 PM, ychawla <[hidden email]>wrote:

> Hi Amby,
> What about using a simple XSLT to go from the outputted format to your
> desired format?  It should be a pretty straight forward transformation.
>
> Thanks,
> Yogesh
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/Converting-CSV-to-XML-tp5711086p5711598.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>



If you reply to this email, your message will be added to the discussion below:
http://camel.465427.n5.nabble.com/Converting-CSV-to-XML-tp5711086p5711606.html
To start a new topic under Camel - Users, email [hidden email]
To unsubscribe from Camel - Users, click here.
NAML

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Converting CSV to XML

Christian Mueller
In reply to this post by ambarish.d
My recommendations:
1) I prefer using JAXB instead of XStream.

2) Modify your route so they looks like:
JaxbDataFormat jaxb = new JaxbDataFormat("com.mycompany...");
from("")
  .unmarshall().csv()
  .convertBodyTo(List.class) // see [1]
  .marshal(jaxb)
  .to("");

[1] http://camel.apache.org/type-converter.html

Best,
Christian

On Wed, May 16, 2012 at 9:55 PM, ambarish.d <[hidden email]> wrote:

> Hello,
>
> I want to convert a CSV file to XML file in proper format. I have following
> code which is doing the Job but output is not a expected one.
>
>  context.addRoutes(new RouteBuilder() {
>                    public void configure() {
>
>  from("file://TEST?fileName=ArticleDistribution.csv").
>                        unmarshal().
>                        csv().
>                        marshal().
>                        xstream().
>
>  to("file://TESTOUT?fileName=ArticleDistribution.XML");
>                    }
>                });
>
> For Example:
> My CSV File looks like this:
>
> Name, Age, Sex, Zip
> Edward, 22,M,33639
> Ema,27,F,30330
>
> Output of Above code is:
>
> <list>
>  <java.util.Arrays_-ArrayList>
>
>      <string>Name</string>
>      <string>Age</string>
>      <string>Sex</string>
>      <string>Zip</string>
>
>  </java.util.Arrays_-ArrayList>
> <java.util.Arrays_-ArrayList>
>
>      <string>Edward</string>
>      <string> 22</string>
>      <string>M</string>
>      <string>33639</string>
>
>  </java.util.Arrays_-ArrayList>
> ..
> </List>
>
> But Output I want is
>
> <List>
> <data>
> <Name>Edward</Name>
> <Age>22</Age>
> <Sex>M</Sex>
> <Zip>33639</Zip>
> </data>
> <data>
> ....
> </data>
> </List>
>
> Can anybody please help me and show the proper way of doing this. It will
> be
> a great help.
>
> thanks
> Amby
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/Converting-CSV-to-XML-tp5711086.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Converting CSV to XML

ambarish.d
In reply to this post by ramesri
Yogesh,
 
Actually, I am looking for more generic solution which will take any CSV as input and convert it into required XML format. So is the reason I cant use XSLT in this case.
 
Its my bad that I didnt specified this in original problem description. Please let me know if I am wrong in understanding ur point.
 
Ramesh,
 
Can you please throw some more light on ur suggested solution? It will be a great help.
 
thanks
Amby
 
 
 


 
On Thu, May 17, 2012 at 4:32 PM, ramesri [via Camel] <[hidden email]> wrote:
Yogesh, you can use camel flatpack component to parse the csv records.

And then use a Velocity template to output it to desired xml.

Thanks
Ramesh

On Thu, May 17, 2012 at 4:24 PM, Christian Mueller [via Camel] <[hidden email]> wrote:
You cannot use XSLT to transform a CSV file into XML...


On Thu, May 17, 2012 at 10:19 PM, ychawla <[hidden email]>wrote:

> Hi Amby,
> What about using a simple XSLT to go from the outputted format to your
> desired format?  It should be a pretty straight forward transformation.
>
> Thanks,
> Yogesh
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/Converting-CSV-to-XML-tp5711086p5711598.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>



If you reply to this email, your message will be added to the discussion below:
http://camel.465427.n5.nabble.com/Converting-CSV-to-XML-tp5711086p5711606.html
To start a new topic under Camel - Users, email [hidden email]
To unsubscribe from Camel - Users, click here.
NAML




If you reply to this email, your message will be added to the discussion below:
http://camel.465427.n5.nabble.com/Converting-CSV-to-XML-tp5711086p5711614.html
To unsubscribe from Converting CSV to XML, click here.
NAML

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Converting CSV to XML

Claus Ibsen-2
In reply to this post by ambarish.d
Hi

See Bindy, and then you can annotate the bindy model class with JAXB
annotations, and then convert to XML.
http://camel.apache.org/bindy

Jonathan's article has an example showing this.
http://java.dzone.com/articles/open-source-integration-apache

On Wed, May 16, 2012 at 9:55 PM, ambarish.d <[hidden email]> wrote:

> Hello,
>
> I want to convert a CSV file to XML file in proper format. I have following
> code which is doing the Job but output is not a expected one.
>
>  context.addRoutes(new RouteBuilder() {
>                    public void configure() {
>                        from("file://TEST?fileName=ArticleDistribution.csv").
>                        unmarshal().
>                        csv().
>                        marshal().
>                        xstream().
>                        to("file://TESTOUT?fileName=ArticleDistribution.XML");
>                    }
>                });
>
> For Example:
> My CSV File looks like this:
>
> Name, Age, Sex, Zip
> Edward, 22,M,33639
> Ema,27,F,30330
>
> Output of Above code is:
>
> <list>
>  <java.util.Arrays_-ArrayList>
>
>      <string>Name</string>
>      <string>Age</string>
>      <string>Sex</string>
>      <string>Zip</string>
>
>  </java.util.Arrays_-ArrayList>
> <java.util.Arrays_-ArrayList>
>
>      <string>Edward</string>
>      <string> 22</string>
>      <string>M</string>
>      <string>33639</string>
>
>  </java.util.Arrays_-ArrayList>
> ..
> </List>
>
> But Output I want is
>
> <List>
> <data>
> <Name>Edward</Name>
> <Age>22</Age>
> <Sex>M</Sex>
> <Zip>33639</Zip>
> </data>
> <data>
> ....
> </data>
> </List>
>
> Can anybody please help me and show the proper way of doing this. It will be
> a great help.
>
> thanks
> Amby
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Converting-CSV-to-XML-tp5711086.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



--
Claus Ibsen
-----------------
CamelOne 2012 Conference, May 15-16, 2012: http://camelone.com
FuseSource
Email: [hidden email]
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Converting CSV to XML

ambarish.d
In reply to this post by ambarish.d
Hello Claus,

Thanks for the help. Bindy was really helpful.
I am still using xstream though, its lot more easier (I feel).

Giving the working solution of this problem if anybody needs one.

 DataFormat bindy = new BindyCsvDataFormat("com.your.package.dto");
            from("file://TEST?fileName=Employee.csv").
                            unmarshal(bindy).
                            marshal().
                        xstream().
                        to("file://TESTOUT?fileName=employee.xml");



This works as expected. "com.your.package.dto.EmployeeDTO" has to be annotated properly to make this work.

List of dependencies:

camel-bindy-2.9.0.jar, camel-core-2.9.0.jar,camel-csv-2.9.0.jar, camel-xstream-2.9.0.jar,
slf4j-api-1.6.4.jar, slf4j-log4j12-1.6.4.jar, solr-commons-csv-1.3.0.jar,xstream-1.3.1
 
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Converting CSV to XML

German O
This post was updated on .
Hi, Is there a way to do this without passing thru a model?... just read the csv, and put in the that format?..

My case is that the csv can change the columns, and then I have to output another xml, so I use a xsl to translate to the final XML.

The idea is to put the csv into a simple XML like the one described here, and with that simple format, I can easy do the xsl to create the final xmls.

thanks.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Converting CSV to XML

Christian Mueller
Use the file component to read the file, access the in message body and do
what ever you need to do.

Best,
Christian

Sent from a mobile device
Am 27.09.2012 07:38 schrieb "German O" <[hidden email]>:

> Hi, Is there a way to do this without passing throw a model?... just read
> the
> csv, and put in the that format?..
>
> My case is that the csv can change the columns, and then I have to output
> another xml, so I use a xsl to translate to the final XML.
>
> The idea is to put the csv into a simple XML like the one described here,
> and with that simple format, I can easy do the xsl to create the final
> xmls.
>
> thanks.
>
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/Converting-CSV-to-XML-tp5711086p5720047.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Converting CSV to XML

Claus Ibsen-2
In reply to this post by German O
On Wed, Sep 26, 2012 at 8:33 PM, German O <[hidden email]> wrote:
> Hi, Is there a way to do this without passing throw a model?... just read the
> csv, and put in the that format?..
>
> My case is that the csv can change the columns, and then I have to output
> another xml, so I use a xsl to translate to the final XML.
>
> The idea is to put the csv into a simple XML like the one described here,
> and with that simple format, I can easy do the xsl to create the final xmls.
>

You can use a bean or template engine to transform the file input to
your XML like structure.
Can turn the file into a List<List> which you can then loop in the
template to output the xml.

Some template engines are:
http://camel.apache.org/velocity
http://camel.apache.org/freemarker

Using a bean is of course just plain java code, and you can then make
the xml structure in code.

Using a CSV component like
http://camel.apache.org/csv

And there is other CSV and XML related components / data format
http://camel.apache.org/data-format.html


> thanks.
>
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Converting-CSV-to-XML-tp5711086p5720047.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



--
Claus Ibsen
-----------------
Red Hat, Inc.
FuseSource is now part of Red Hat
Email: [hidden email]
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen
Loading...