gpt4 book ai didi

java - 使用 Camel 将 XML 转为 Json

转载 作者:数据小太阳 更新时间:2023-10-29 02:09:54 27 4
gpt4 key购买 nike

我的程序应该将 XML 文件格式转换为 JSON 文件格式。我正在尝试使用 marshal 命令,但出现错误:

Exception in thread "main"org.apache.camel.FailedToCreateRouteException: Failed to create routeroute1 at: >>>Marshal[org.apache.camel.model.dataformat.XmlJsonDataFormat@815b41f]<<< in route: Route(route1)[[From[file:resource/inbox]] ->[Marshal[org.ap... because of Data format 'xmljson' could not becreated. Ensure that the data format is valid and the associated Camelcomponent is present on the classpath

Caused by:java.lang.IllegalArgumentException: Data format 'xmljson' could not becreated. Ensure that the data format is valid and the associated Camelcomponent is present on the classpath

Java代码

package route;

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.model.dataformat.XmlJsonDataFormat;
import org.apache.camel.spi.DataFormat;
import org.apache.log4j.BasicConfigurator;

public class CopyToJsonRoute {

public static void main(String args[]) throws Exception {
// Log 4j
BasicConfigurator.configure();

// create CamelContext
CamelContext context = new DefaultCamelContext();

// add our route to the CamelContext
context.addRoutes(new RouteBuilder() {
public void configure() {
// http://www.yr.no/place/Norway/Oslo/Oslo/Oslo/forecast.xml

XmlJsonDataFormat xmlJsonFormat = new XmlJsonDataFormat();
xmlJsonFormat.setForceTopLevelObject(true);
from("file:resource/inbox")
.marshal(xmlJsonFormat)
.to("file:src/main/resources/data/output?autoCreate=true");



}
});

// start the route and let it do its work
context.start();
Thread.sleep(10000);

// stop the CamelContext
context.stop();
}
}

Pom文件

<?xml version="1.0" encoding="UTF-8"?>
<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>com.yourdomain.subdomain</groupId>
<artifactId>copy-to-json</artifactId>
<version>1.0-SNAPSHOT</version>


<dependencies>

<!-- https://mvnrepository.com/artifact/org.apache.camel/camel-core -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>2.20.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.camel/camel-jackson -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jackson</artifactId>
<version>2.20.0</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.8.0-alpha2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.camel/camel-jms -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jms</artifactId>
<version>2.20.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/xom/xom -->
<dependency>
<groupId>xom</groupId>
<artifactId>xom</artifactId>
<version>1.2.5</version>
</dependency>



</dependencies>
</project>

最佳答案

你需要在你的 Maven pom 文件中添加 camel-xmljson 作为依赖

    <dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-xmljson</artifactId>
<version>2.20.0</version>
</dependency>

关于java - 使用 Camel 将 XML 转为 Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46810292/

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