JiBX
Available as of Camel 2.6
JiBX
XStream
XStream is a Data Format which uses the XStream JiBX library to marshal and unmarshal Java objects to and from XML.
Code Block |
---|
// lets turn Object messages into XML then send to MQSeries from("activemq:My.Queue"). marshal().xstreamjibx(). to("mqseries:Another.Queue"); |
XMLInputFactory and XMLOutputFactory
The XStream library uses the javax.xml.stream.XMLInputFactory
and javax.xml.stream.XMLOutputFactory
, you can control which implementation of this factory should be used.
The Factory is discovered using this algorithm:
1. Use the javax.xml.stream.XMLInputFactory
, javax.xml.stream.XMLOutputFactory
system property.
2. Use the lib/xml.stream.properties
file in the JRE_HOME
directory.
3. Use the Services API, if available, to determine the classname by looking in the META-INF/services/javax.xml.stream.XMLInputFactory
, META-INF/services/javax.xml.stream.XMLOutputFactory
files in jars available to the JRE.
4. Use the platform default XMLInputFactory,XMLOutputFactory instance.
How to set the XML encoding in Xstream DataFormat?
From Camel 1.6.3 or Camel 2.2.0, you can set the encoding of XML in Xstream DataFormat by setting the Exchange's property with the key Exchange.CHARSET_NAME
, or setting the encoding property on Xstream from DSL or Spring config.
Please note that marshaling process can recognize the message type at the runtime. However while unmarshaling message from XML we need to specify target class explicitly.
Code Block |
---|
// lets turn XML into PurchaseOrder message
from("mqseries:Another |
Code Block |
from("activemq:My.Queue"). marshalunmarshal().xstream("UTF-8"jibx(PurchaseOrder.class). to("mqseriesactivemq:AnotherMy.Queue"); |
Wiki Markup |
---|
{snippet:id=e1|lang=xml|url=camel/trunk/components/camel-xstream/src/test/resources/org/apache/camel/dataformat/xstream/SpringMarshalListTest.xml} |
Dependencies
JiBX Spring DSL
JiBX data format is also supported by Camel Spring DSL.
Code Block | ||||
---|---|---|---|---|
| ||||
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
<!-- Define data formats -->
<dataFormats>
<jibx id="jibx" unmarshallClass="org.apache.camel.dataformat.jibx.PurchaseOrder"/>
</dataFormats>
<!-- Marshal message to XML -->
<route>
<from uri="direct:marshal"/>
<marshal ref="jibx"/>
<to uri="mock:result"/>
</route>
<!-- Unmarshal message from XML -->
<route>
<from uri="direct:unmarshal"/>
<unmarshal ref="jibx"/>
<to uri="mock:result"/>
</route>
</camelContext>
|
Dependencies
To use JiBX To use XStream in your camel routes you need to add the a dependency on camel-xstreamjibx which implements this data format.
If you use maven you could just add the following to your pom.xml, substituting the version number for the latest & greatest release (see the download page for the latest versions).
Code Block | ||||
---|---|---|---|---|
| ||||
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-xstream<jibx</artifactId> <version>1<version>2.56.0</version> </dependency> |