2012-09-04 52 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>