java - 为什么这个 Apache Camel 路由不将 JSON 转换为 XML?

标签 java json xml apache-camel

我正在尝试使用 Apache Camel 路由将一些 JSON 转换为 XML。下面是包含路由的代码, list 1。调用此代码的代码是第二个源代码段, list 2。我要转换为 XML 的 JSON 在 list 3 中。从Apache Camel 路由在 list 4 中。实际创建的 XML 不是 JSON 数据。 有人知道如何使用 Apache Camel 将 JSON 转换为 XML 吗?

package org.hai;

import org.apache.camel.builder.RouteBuilder;

/**
 * A Camel Java DSL Router
 */
public class MyRouteBuilder extends RouteBuilder {

    /**
     * Let's configure the Camel routing rules using Java code...
     */
    public void configure() {

        // Changes JSON data found in folder to XML - (hopefully) 
        from("file:src/data?noop=true").marshal().xstream().to("file:target/messages/others");
    }

}

list 1:将 JSON 更改为 XML 的 Apache Camel 路由

package org.hai;

import org.apache.camel.main.Main;

/**
 * A Camel Application
 */
public class MainApp {

    /**
     * A main() so we can easily run these routing rules in our IDE
     */
    public static void main(String... args) throws Exception {
        Main main = new Main();
        main.enableHangupSupport();
        main.addRouteBuilder(new MyRouteBuilder());
        main.run(args);

        Thread.sleep(5000);

        System.exit(1);
    }

}

list 2:调用 Apache Camel 路由的代码。

{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}

list 3:要转换为 XML 的 JSON

<?xml version='1.0' encoding='UTF-8'?>
<org.apache.camel.component.file.GenericFile>
<endpointPath>src/data</endpointPath>
<fileName>example.xml</fileName>
<fileNameOnly>example.xml</fileNameOnly>
<relativeFilePath>example.xml</relativeFilePath>
<absoluteFilePath>/home/captainkyle/Desktop/MavenPractice/transformer2/src/data/example.xml</absoluteFilePath>
<fileLength>583</fileLength>
<lastModified>1434061793000</lastModified>
<file class="file">src/data/example.xml</file>
<binding class="org.apache.camel.component.file.FileBinding"/>
<absolute>false</absolute>
<directory>false</directory>
</org.apache.camel.component.file.GenericFile>

list 4:从上述 Camel 路由创建的 XML。

感谢您阅读本文。

问候,

最佳答案

看起来您缺少先将 json 文本转换为对象的步骤。例如,

from("file:src/data?noop=true").unmarshal().json(JsonLibrary.Jackson, Map.class).marshal().xstream().to("file:target/messages/others");

您可能希望转换为 POJO(而不是 Map)以避免难看的 xml 呈现。

您需要在应用程序依赖项中包含 camel-jackson 库(如果您愿意,也可以包含 camel-gson)。

关于java - 为什么这个 Apache Camel 路由不将 JSON 转换为 XML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30806610/

相关文章:

Java Reg-ex 验证文件约定

python - yFinance 的 JSON 解码错误 [JSONDecodeError : Expecting value: line 1 column 1 (char 0)]

jquery - 如何在 ASP.NET MVC 中将 flot 与 jQuery 结合使用?

xml - 将整数值转换为重复字符

xml - 我的 XSLT 中有什么错误?

java - 当我启动线程时,运行没有被调用。 java

java - 如何实现一个鼠标监听器,返回鼠标在 JTable 中指向的字符?

java - oracle.sql.CLOB 与 java.sql.Clob

sql - SQL Server 中 json 列的默认值应该使用什么?

xml - 如何在使用 xslt 对元素求和期间处理空元素