Why is this Apache Camel route not converting JSON to XML?

I am trying to convert some JSON to XML using Apache Camel route. Below is the code with the route in it: 1. The code that calls this code is the second source code segment containing the list 2. The JSON that I want to convert to XML is shown in Listing 3. The XML that is generated from the Apache Camel Route is in Listing 4. The actual generated XML is not JSON data. Does anyone know how I can convert JSON to XML with Apache Camel?

package org.hai;

import org.apache.camel.builder.RouteBuilder;

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

    /**
     * Let 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");
    }

}

      

Listing 1: Apache Camel Route to Change JSON to XML

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);
    }

}

      

Listing 2: Code that calls the Apache Camel route.

{
    "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"
                }
            }
        }
    }
}

      

Listing 3: JSON is converted to XML

<?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>

      

Listing 4: XML generated with the above Camel route.

Thanks for this.

Hello,

+3


source to share


1 answer


It looks like you are missing a step to first convert json text to object. For example.

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

      



You can convert to POJO (not map) to avoid ugly xml rendering.

You will need a camel-jackson library (or camel-gson if you prefer) depending on your application.

+2


source







All Articles