gpt4 book ai didi

java - 使用 apache Camel 将文件类型从 csv 转换为 xml

转载 作者:行者123 更新时间:2023-12-02 06:06:48 25 4
gpt4 key购买 nike

我正在尝试使用 apache Camel 将 csv 文件转换为 xml 文件,我在编码和解码中遇到问题......这个程序的代码是......

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

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

提前致谢......

最佳答案

  1. 我在本地计算机上测试了具有以下依赖项的相同示例。
  2. 如果您在 Windows 中运行它,文件路径不正确。

检查下面的代码。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?fileName = input.csv

这对我有用。

关于java - 使用 apache Camel 将文件类型从 csv 转换为 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22213536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com