1 Reply Latest reply on Apr 25, 2015 3:30 AM by bharadwaj

    How do convert xml to csv in camel

    szarekar

      I need to convert the xml to csv in Apache camel .Can anyone please help.


      <?xml version='1.0' encoding='UTF-8'?>
      <list>
      <map>
      <entry>
      <string>com.fusesource.sample.dto.EmployeeDTO</string><com.fusesource.sample.dto.EmployeeDTO>
      <employeeId>23445</employeeId>
      <firstName>Ambarish</firstName>
      <lastName>Deshpande</lastName>
      <role>Java Lead</role>
      </com.fusesource.sample.dto.EmployeeDTO>
      </entry>
      </map>
      <map>
      <entry>
      <string>com.fusesource.sample.dto.EmployeeDTO</string><com.fusesource.sample.dto.EmployeeDTO>
      <employeeId>23411</employeeId>
      <firstName>Paul</firstName>
      <lastName>Bulson</lastName>
      <role>Team Member</role>
      </com.fusesource.sample.dto.EmployeeDTO>
      </entry>
      </map>
      </list>



      ######


      I am trying to understand how xslt will help in this case?


      I found this information on internet - it says

      xslt:templateName[?options]

      Where templateName is the classpath-local URI of the template to invoke; or the complete URL of the remote template

      what exactly is  template ? and how will it help me to convert xml to csv ?


      from("activemq:My.Queue"). to("xslt:com/acme/mytransform.xsl");
      what does the above sample code do ?
        • 1. Re: How do convert xml to csv in camel
          bharadwaj

          <camelContext xmlns="http://camel.apache.org/schema/spring">
            
          <route>
            
          <from uri="file:src/xmldata?noop=true"/>
            
          <to uri="xslt:file:src/main/fruits.xslt"/>
            
          <to uri="file://TESTOUT?fileName=output.csv"/>
            
          </route>
           
          </camelContext>

          sample xml file in src/xmldata folder

          <AllFruits xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
          <!-- All fruits below. -->
          <Fruit>
           
          <FruitId>Bannana</FruitId>
           
          <FruitColor>Yellow</FruitColor>
           
          <FruitShape>Moon</FruitShape>
           
          <Customer>
            
          <Name>Joe</Name>
            
          <NumberEaten>5</NumberEaten>
            
          <Weight>2.6</Weight>
           
          </Customer>
           
          <Customer>
            
          <Name>Mark</Name>
            
          <NumberEaten>8</NumberEaten>
            
          <Weight>5.0</Weight>
           
          </Customer>
          </Fruit>
          </AllFruits>

          src/main/fruits.xslt

          <?xml version="1.0"?>
          <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            version
          ="1.0">
            
          <xsl:output method="text" encoding="ISO-8859-1" />
            
          <xsl:variable name="newline" select="'&#xA;'"/>
            
          <xsl:template match="Fruit">
            
          <xsl:for-each select="Customer">
            
          <xsl:value-of select="preceding-sibling::FruitId" />
            
          <xsl:text>,</xsl:text>
            
          <xsl:value-of select="NumberEaten" />
            
          <xsl:text>,</xsl:text>
            
          <xsl:value-of select="Weight" />
            
          <xsl:value-of select="$newline" />
            
          </xsl:for-each>
            
          </xsl:template>
          </xsl:stylesheet>