2012-09-04 87 views
-1

我想使用camel將.xml文件創建爲csv文件。這裏是我的代碼將xml文件轉換爲csv格式唱歌駱駝?

CamelContext context = new DefaultCamelContext(); 
from("file://Input?fileName=test.xml").marshal().csv().to("file://test?fileName=test.csv"); 
context.start(); 

但它沒有在所需的文件夾中創建任何文件「測試」。

+0

您是否嘗試調試您的代碼?將此行分隔爲4行:每行調用一個方法並查看中間結果 – AlexR

回答

5

請多花一點時間在駱駝文檔上,試一試這些例子,並閱讀FAQ。和介紹文章和whatnot。

上面的代碼甚至不是有效的,因爲您需要將它放在RouteBuilder中。

另外,當您啓動CamelContext時,請閱讀啓動方法的javadoc。並閱讀此FAQ http://camel.apache.org/running-camel-standalone-and-have-it-keep-running.html

駱駝提供了一個跟蹤器,所以你可以看到郵件正在處理的消息流。跟蹤器將默認將此日誌記錄在INFO級別。 http://camel.apache.org/tracer

1

下面是使用駱駝的樣品中的src/XMLDATA夾

<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> 

示例XML文件

<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 /主/ 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>