Nikhil Joshi
Nikhil Joshi

Reputation: 817

Apache Camel XML-to-JSON

I have simple XML file and need to convert it to JSON using camel-xmljson JAR. I have started camel context using:

Main main = new Main();
main.addRouteBuilder(new ConvertXmlToJson());
main.enableHangupSupport();
main.run();

And my configure method looks like:

@Override
public void configure() throws Exception {
    XmlJsonDataFormat xmlJsonFormat = new XmlJsonDataFormat();
    xmlJsonFormat.setForceTopLevelObject(true);

    // from XML to JSON

    //#1
    from("direct:marshal").marshal(xmlJsonFormat).to("mock:json"); 

    //#2
    //from("file:resources/SimpleFile.xml").marshal(xmlJsonFormat).to("file:resources/JsonOutput.txt");
}

Now I am not able to understand where should I exactly pass my xml object? Is #2 looks correct? Nothing happens when I execute any one of them.

It will be fine as well to print converted JSON on the console rather than file.

Thanks in advance for the help.

Upvotes: 1

Views: 7121

Answers (1)

Sergey
Sergey

Reputation: 1361

Just change your route to:

from("file:resource/inbox").marshal(xmlJsonFormat).to("file:resource/outbox");

Then copy SimpleFile.xml into resource/inbox, run the application and you will get JSON in resource/outbox

Upvotes: 1

Related Questions