...
Just instantiate the XmlJsonDataFormat from package org.apache.camel.dataformat.xmljson. Make sure you have installed the camel-xmljson
feature (if running on OSGi) or that you've included camel-xmljson-{version}.jar and its transitive dependencies in your classpath. Example initialization with a default configuration:
Code Block |
|
XmlJsonDataFormat xmlJsonFormat = new XmlJsonDataFormat();
|
To tune the behaviour of the data format as per the options above, use the appropriate setters:
Code Block |
|
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"));
|
Once you've instantiated the data format, the next step is to actually use the it from within the marshal()
and unmarshal()
DSL elements:
Code Block |
|
// from XML to JSON
from("direct:marshal").marshal(xmlJsonFormat).to("mock:json");
// from JSON to XML
from("direct:unmarshal").unmarshal(xmlJsonFormat).to("mock:xml");
|
...
Alternatively, you can define the data format inline by using the xmljson()
DSL element.
Code Block |
|
// from XML to JSON - inline dataformat
from("direct:marshalInline").marshal().xmljson().to("mock:jsonInline");
// from JSON to XML - inline dataformat
from("direct:unmarshalInline").unmarshal().xmljson().to("mock:xmlInline");
|
If you wish, you can even pass in a Map<String, String> to the inline methods to provide custom options:
Code Block |
|
Map<String, String> xmlJsonOptions = new HashMap<String, String>();
xmlJsonOptions.put(org.apache.camel.model.dataformat.XmlJsonDataFormat.ENCODING, "UTF-8");
xmlJsonOptions.put(org.apache.camel.model.dataformat.XmlJsonDataFormat.ROOT_NAME, "newRoot");
xmlJsonOptions.put(org.apache.camel.model.dataformat.XmlJsonDataFormat.SKIP_NAMESPACES, "true");
xmlJsonOptions.put(org.apache.camel.model.dataformat.XmlJsonDataFormat.REMOVE_NAMESPACE_PREFIXES, "true");
xmlJsonOptions.put(org.apache.camel.model.dataformat.XmlJsonDataFormat.EXPANDABLE_PROPERTIES, "d e");
// from XML to JSON - inline dataformat w/ options
from("direct:marshalInlineOptions").marshal().xmljson(xmlJsonOptions).to("mock:jsonInlineOptions");
// form JSON to XML - inline dataformat w/ options
from("direct:unmarshalInlineOptions").unmarshal().xmljson(xmlJsonOptions).to("mock:xmlInlineOptions");
|
...
Within the <dataFormats>
block, simply configure an xmljson
element with unique IDs:
Code Block |
|
<dataFormats>
<xmljson id="xmljson"/>
<xmljson id="xmljsonWithOptions" forceTopLevelObject="true" trimSpaces="true" rootName="newRoot" skipNamespaces="true"
removeNamespacePrefixes="true" expandableProperties="d e"/>
</dataFormats>
|
Then you simply refer to the data format object within your <marshal />
and {<unmarshal />}} DSLs:
Code Block |
|
<route>
<from uri="direct:marshal"/>
<marshal ref="xmljson"/>
<to uri="mock:json" />
</route>
<route>
<from uri="direct:unmarshalWithOptions"/>
<unmarshal ref="xmljsonWithOptions"/>
<to uri="mock:xmlWithOptions"/>
</route>
|
...
To bridge the gap, Json-lib has an option to bind namespace declarations in the form of prefixes and namespace URIs to XML output elements while unmarshalling (i.e. converting from JSON to XML). For example, provided the following JSON string:
Code Block |
{ 'pref1:a': 'value1', 'pref2:b': 'value2 }
|
...
The full code would look like that:
Code Block |
|
XmlJsonDataFormat namespacesFormat = new XmlJsonDataFormat();
List<XmlJsonDataFormat.NamespacesPerElementMapping> namespaces = new ArrayList<XmlJsonDataFormat.NamespacesPerElementMapping>();
namespaces.add(new XmlJsonDataFormat.
NamespacesPerElementMapping("", "|ns1|http://camel.apache.org/test1||http://camel.apache.org/default|"));
namespaces.add(new XmlJsonDataFormat.
NamespacesPerElementMapping("surname", "|ns2|http://camel.apache.org/personalData|" +
"ns3|http://camel.apache.org/personalData2|"));
namespacesFormat.setNamespaceMappings(namespaces);
namespacesFormat.setRootElement("person");
|
...
Using the namespace bindings in the Java snippet above on the following JSON string:
Code Block |
|
{ "name": "Raul", "surname": "Kripalani", "f": true, "g": null}
|
Would yield the following XML:
Code Block |
|
<person xmlns="http://camel.apache.org/default" xmlns:ns1="http://camel.apache.org/test1">
<f>true</f>
<g null="true"/>
<name>Raul</name>
<surname xmlns:ns2="http://camel.apache.org/personalData" xmlns:ns3="http://camel.apache.org/personalData2">Kripalani</surname>
</person>
|
...
To use the XmlJson dataformat in your camel routes you need to add the following dependency to your pom.
Code Block |
|
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-xmljson</artifactId>
<version>x.x.x</version>
<!-- Use the same version as camel-core, but remember that this component is only available from 2.10 onwards -->
</dependency>
|
...