Adam Davies Adam Davies - 4 months ago 31
JSON Question

Apache Camel XmlJsonDataFormat XML to JSON

I have the following code, which I have taken from the Apache Camel web site:

XmlJsonDataFormat xmlJsonFormat = new XmlJsonDataFormat();
xmlJsonFormat.setEncoding("UTF-8");
xmlJsonFormat.setForceTopLevelObject(true);
xmlJsonFormat.setTrimSpaces(true);
xmlJsonFormat.setRootName("newRoot");
xmlJsonFormat.setSkipNamespaces(true);
xmlJsonFormat.setRemoveNamespacePrefixes(true);
xmlJsonFormat.setExpandableProperties(Arrays.asList("d", "e"));

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


At the last line (marked 1), I get a compile error. I don't understand where the
from
method is coming from; there are no static imports, and I've looked through the examples and the postings on this site.

I don't know anything about Camel, and want to run this as a stand alone utility class for doing simple xml -> json conversions.

Can anyone help me understand what I have to do to make this snippet work. Failing that, does anyone know of a simple xml to json converter?

Answer

A good idea to get started with Camel is to walk throught this getting started guide

Also reading this article that excellent explain Camel in a 5 minute read is recommended

Then you would know that the from is from a Camel route, and that you need to use a RouteBuilder class to use it.