使用apache camel从csv到xml的文件类型转换

时间:2014-03-06 02:17:33

标签: java apache-camel

我正在尝试使用apache camel将csv文件转换为xml文件我面临着mar ining和unmarshalling的问题....这个程序的代码是......

package com.mycompany.camel.java;

import java.io.Serializable;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

import org.apache.camel.dataformat.bindy.annotation.CsvRecord;
import org.apache.camel.dataformat.bindy.annotation.DataField;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@CsvRecord(separator = ",", skipFirstLine = true)

public class EmployeeDTO implements Serializable{
@XmlAttribute
      @DataField(pos = 1)
      private int employeeId;
      @XmlAttribute
      @DataField(pos = 2)
      private String firstName;
      @XmlAttribute
      @DataField(pos = 3)
      private String lastName;
      @XmlAttribute
      @DataField(pos = 4)
      private String role;
}


package com.mycompany.camel.java;

import org.apache.camel.CamelContext;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.dataformat.bindy.csv.BindyCsvDataFormat;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.spi.DataFormat;

public class ConvertorRoute implements RoutesBuilder{

@Override
public void addRoutesToCamelContext(CamelContext context) throws Exception {
  context.addRoutes(new RouteBuilder() {
public void configure() {
  try {
            DataFormat bindy = new BindyCsvDataFormat("com.mycompany.camel.java");
                  from("file://C:/Users/rahul/Desktop/Employee.csv").
                  unmarshal(bindy).
                  marshal().
                  xstream().
                  to("file://C:/Users/rahul/Desktop/employee.xml");
            } catch (Exception e) {
                        e.printStackTrace();
            }
         }
     });
}


public static void main(String[] args) { 
     try{
    CamelContext context = new DefaultCamelContext();
          ConvertorRoute route = new ConvertorRoute();
          route.addRoutesToCamelContext(context);
          context.start();
          Thread.sleep(5000);
          context.stop();
      }catch(Exception exe){
          exe.printStackTrace();
    }

    }
}

我面临的错误是:

org.apache.camel.FailedToCreateRouteException: Failed to create route route1 at: >>> Marshal[org.apache.camel.model.dataformat.XStreamDataFormat@1aa9f99] <<< in route: Route[[From[file://C:/Users/JaHnAvI/Desktop?fileName=Employe... because of Data format 'xstream' could not be created. Ensure that the data format is valid and the associated Camel component is present on the classpath
at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:879)
at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:172)
at org.apache.camel.impl.DefaultCamelContext.startRoute(DefaultCamelContext.java:722)
at org.apache.camel.impl.DefaultCamelContext.startRouteDefinitions(DefaultCamelContext.java:1789)
at org.apache.camel.impl.DefaultCamelContext.doStartCamel(DefaultCamelContext.java:1575)
at org.apache.camel.impl.DefaultCamelContext.doStart(DefaultCamelContext.java:1444)
at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:60)
at org.apache.camel.impl.DefaultCamelContext.start(DefaultCamelContext.java:1412)
at com.mycompany.camel.java.ConvertorRoute.main(ConvertorRoute.java:36)
 Caused by: java.lang.IllegalArgumentException: Data format 'xstream' could not be created. Ensure that the data format is valid and the associated Camel component is present on the classpath
at org.apache.camel.model.DataFormatDefinition.getDataFormat(DataFormatDefinition.java:89)
at org.apache.camel.model.DataFormatDefinition.getDataFormat(DataFormatDefinition.java:79)
at org.apache.camel.model.MarshalDefinition.createProcessor(MarshalDefinition.java:150)
at org.apache.camel.model.ProcessorDefinition.makeProcessor(ProcessorDefinition.java:461)
at org.apache.camel.model.ProcessorDefinition.addRoutes(ProcessorDefinition.java:179)
at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:876)
... 8 more


org.apache.camel.FailedToCreateRouteException: Failed to create route route1 at: >>> Marshal[org.apache.camel.model.dataformat.XStreamDataFormat@1aa9f99] <<< in route: Route[[From[file://C:/Users/JaHnAvI/Desktop?fileName=Employe... because of Data format 'xstream' could not be created. Ensure that the data format is valid and the associated Camel component is present on the classpath
    at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:879)
    at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:172)
    at org.apache.camel.impl.DefaultCamelContext.startRoute(DefaultCamelContext.java:722)
    at org.apache.camel.impl.DefaultCamelContext.startRouteDefinitions(DefaultCamelContext.java:1789)
    at org.apache.camel.impl.DefaultCamelContext.doStartCamel(DefaultCamelContext.java:1575)
    at org.apache.camel.impl.DefaultCamelContext.doStart(DefaultCamelContext.java:1444)
    at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:60)
    at org.apache.camel.impl.DefaultCamelContext.start(DefaultCamelContext.java:1412)
    at com.mycompany.camel.java.ConvertorRoute.main(ConvertorRoute.java:36)
Caused by: java.lang.IllegalArgumentException: Data format 'xstream' could not be created. Ensure that the data format is valid and the associated Camel component is present on the classpath
    at org.apache.camel.model.DataFormatDefinition.getDataFormat(DataFormatDefinition.java:89)
    at org.apache.camel.model.DataFormatDefinition.getDataFormat(DataFormatDefinition.java:79)
    at org.apache.camel.model.MarshalDefinition.createProcessor(MarshalDefinition.java:150)
    at org.apache.camel.model.ProcessorDefinition.makeProcessor(ProcessorDefinition.java:461)
    at org.apache.camel.model.ProcessorDefinition.addRoutes(ProcessorDefinition.java:179)
    at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:876)
    ... 8 more

请帮我解决这个错误.....

提前致谢.....

2 个答案:

答案 0 :(得分:1)

您是否在maven文件中包含了Xstream的依赖项?如果您使用的是maven,请将以下内容添加到项目文件中:

<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-xstream</artifactId>
  <version>x.x.x</version>
  <!-- use the same version as your Camel core version -->
</dependency>

错误清楚地表明您缺少组件

答案 1 :(得分:1)

  1. 我在本地计算机上使用以下依赖项测试了相同的示例。
  2. 如果您在Windows中运行文件路径,则
  3. 文件路径不正确。
  4. 检查下面的代码。 的pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>CamelBasic</groupId>
      <artifactId>CamelBasic</artifactId>
      <version>0.0.1-SNAPSHOT</version>
    
       <properties>
            <camel-version>2.12.0</camel-version>
        </properties>
    
      <dependencies>
        <dependency>
           <groupId>org.apache.camel</groupId>
           <artifactId>camel-core</artifactId>
           <version>${camel-version}</version>
        </dependency>
        <dependency>
           <groupId>org.apache.camel</groupId>
           <artifactId>camel-csv</artifactId>
           <version>${camel-version}</version>
        </dependency>
        <dependency>
           <groupId>org.apache.camel</groupId>
           <artifactId>camel-xstream</artifactId>
           <version>${camel-version}</version>
        </dependency>
        <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.16</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>1.7.5</version>
            </dependency>
            <dependency>
                <groupId>com.google.guava</groupId>
                <artifactId>guava</artifactId>
                <version>14.0.1</version>
            </dependency>
            <dependency>
                <groupId>org.apache.camel</groupId>
                <artifactId>camel-bindy</artifactId>
                <version>${camel-version}</version>
            </dependency>
      </dependencies>
    
      <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
              <source>1.6</source>
              <target>1.6</target>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </project>
    

    路径示例: 文件:d:\ CamelTestData \ csvDir文件名= input.csv

    它对我有用。