java - 使用 Camel 将 XML 转为 Json

标签 java json xml maven apache-camel

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

Exception in thread "main" org.apache.camel.FailedToCreateRouteException: Failed to create route route1 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 be created. Ensure that the data format is valid and the associated Camel component is present on the classpath

Caused by: java.lang.IllegalArgumentException: Data format 'xmljson' could not be created. Ensure that the data format is valid and the associated Camel component 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/

相关文章:

java - Android/Java人脸识别: gender,年龄、情绪

java - 如何将ArrayList的索引添加到同一个Arraylist Java

java - 未找到用户类型 (JPA) 的属性姓氏

android - ScrollView 无法显示最后一个元素

xml - Adobe XFL Flash 交换格式的文档在哪里?

.net - 如何实现简单的XPath查找

java - 如果对象引用是静态的,是否意味着该对象的属性也是静态的?

javascript - 无法访问 JSON 数组中的项目?

ios - 获取 Facebook API 个人资料图片 ios Swift

javascript - 用 str.replace 制作一个好的 json