Modified: websites/production/camel/content/xpath.html
==============================================================================
--- websites/production/camel/content/xpath.html (original)
+++ websites/production/camel/content/xpath.html Fri Aug 25 08:22:01 2017
@@ -36,17 +36,6 @@
<![endif]-->
- <link href='//camel.apache.org/styles/highlighter/styles/shCoreCamel.css'
rel='stylesheet' type='text/css' />
- <link href='//camel.apache.org/styles/highlighter/styles/shThemeCamel.css'
rel='stylesheet' type='text/css' />
- <script src='//camel.apache.org/styles/highlighter/scripts/shCore.js'
type='text/javascript'></script>
- <script src='//camel.apache.org/styles/highlighter/scripts/shBrushJava.js'
type='text/javascript'></script>
- <script src='//camel.apache.org/styles/highlighter/scripts/shBrushXml.js'
type='text/javascript'></script>
- <script src='//camel.apache.org/styles/highlighter/scripts/shBrushPlain.js'
type='text/javascript'></script>
-
- <script type="text/javascript">
- SyntaxHighlighter.defaults['toolbar'] = false;
- SyntaxHighlighter.all();
- </script>
<title>
Apache Camel: XPath
@@ -86,283 +75,58 @@
<tbody>
<tr>
<td valign="top" width="100%">
-<div class="wiki-content maincontent"><h2 id="XPath-XPath">XPath</h2><p>Camel
supports <a shape="rect" class="external-link"
href="http://www.w3.org/TR/xpath" rel="nofollow">XPath</a> to allow an <a
shape="rect" href="expression.html">Expression</a> or <a shape="rect"
href="predicate.html">Predicate</a> to be used in the <a shape="rect"
href="dsl.html">DSL</a> or <a shape="rect" href="xml-configuration.html">Xml
Configuration</a>. For example you could use XPath to create an <a shape="rect"
href="predicate.html">Predicate</a> in a <a shape="rect"
href="message-filter.html">Message Filter</a> or as an <a shape="rect"
href="expression.html">Expression</a> for a <a shape="rect"
href="recipient-list.html">Recipient List</a>.</p><div
class="confluence-information-macro
confluence-information-macro-information"><p class="title">Streams</p><span
class="aui-icon aui-icon-small aui-iconfont-info
confluence-information-macro-icon"></span><div
class="confluence-information-macro-body"><p>If th
e message body is stream based, which means the input is received by Camel as
a stream, then you will only be able to read the content of the stream
<strong>once</strong>. Oftentimes when using <a shape="rect"
href="xpath.html">XPath</a> as <a shape="rect"
href="message-filter.html">Message Filter</a> or <a shape="rect"
href="content-based-router.html">Content Based Router</a> the data will be
accessed multiple times. Therefore use <a shape="rect"
href="stream-caching.html">Stream caching</a> or convert the message body to a
<strong><code>String</code></strong> beforehand. This makes it safe to be
re-read multiple times.</p></div></div><div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[from("queue:foo")
- .filter().xpath("//foo"))
- .to("queue:bar")
-]]></script>
-</div></div><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[from("queue:foo")
- .choice().xpath("//foo")).to("queue:bar")
- .otherwise().to("queue:others");
-]]></script>
-</div></div><h3 id="XPath-Namespaces">Namespaces</h3><p>You can easily use
namespaces with XPath expressions using the Namespaces helper class.</p><div
class="code panel pdl" style="border-width: 1px;"><div class="codeContent
panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-Namespaces ns = new Namespaces("c",
"http://acme.com/cheese");
-
-from("direct:start").filter().
- xpath("/c:person[@name='James']", ns).
- to("mock:result");
-]]></script>
-</div></div><h3 id="XPath-Variables">Variables</h3><p>Variables in XPath is
defined in different namespaces. The default namespace is <code><a shape="rect"
class="external-link"
href="http://camel.apache.org/schema/spring">http://camel.apache.org/schema/spring</a></code>.</p><div
class="table-wrap"><table class="confluenceTable"><tbody><tr><th colspan="1"
rowspan="1" class="confluenceTh"><p>Namespace URI</p></th><th colspan="1"
rowspan="1" class="confluenceTh"><p>Local part</p></th><th colspan="1"
rowspan="1" class="confluenceTh"><p>Type</p></th><th colspan="1" rowspan="1"
class="confluenceTh"><p>Description</p></th></tr><tr><td colspan="1"
rowspan="1" class="confluenceTd"><a shape="rect" class="external-link"
href="http://camel.apache.org/xml/in/">http://camel.apache.org/xml/in/</a></td><td
colspan="1" rowspan="1" class="confluenceTd"><p><code>in</code></p></td><td
colspan="1" rowspan="1"
class="confluenceTd"><p><code>Message</code></p></td><td colspan="1"
rowspan="1" class="conflu
enceTd"><p>The <strong><code>exchange.in</code></strong>
message.</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><a
shape="rect" class="external-link"
href="http://camel.apache.org/xml/out/">http://camel.apache.org/xml/out/</a></td><td
colspan="1" rowspan="1" class="confluenceTd"><p><code>out</code></p></td><td
colspan="1" rowspan="1"
class="confluenceTd"><p><code>Message</code></p></td><td colspan="1"
rowspan="1"
class="confluenceTd"><p>The <strong><code>exchange.out</code></strong>
message.</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><a
shape="rect" class="external-link"
href="http://camel.apache.org/xml/function/">http://camel.apache.org/xml/function/</a></td><td
colspan="1" rowspan="1"
class="confluenceTd"><p><code>functions</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>Object</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><strong>Camel 2.5:</strong> Additional
functions.</p></td></
tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><a shape="rect"
class="external-link"
href="http://camel.apache.org/xml/variables/environment-variables">http://camel.apache.org/xml/variables/environment-variables</a></td><td
colspan="1" rowspan="1" class="confluenceTd"><p><code>env</code></p></td><td
colspan="1" rowspan="1" class="confluenceTd"><code>Object</code></td><td
colspan="1" rowspan="1" class="confluenceTd"><p>OS environment
variables.</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><a
shape="rect" class="external-link"
href="http://camel.apache.org/xml/variables/system-properties">http://camel.apache.org/xml/variables/system-properties</a></td><td
colspan="1" rowspan="1"
class="confluenceTd"><p><code>system</code></p></td><td colspan="1" rowspan="1"
class="confluenceTd"><code>Object</code></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>Java System properties.</p></td></tr><tr><td
colspan="1" rowspan="1" class="confluenceTd"><a shape="rect
" class="external-link"
href="http://camel.apache.org/xml/variables/exchange-property">http://camel.apache.org/xml/variables/exchange-property</a></td><td
colspan="1" rowspan="1" class="confluenceTd"><p> </p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>Object</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>The exchange
property.</p></td></tr></tbody></table></div><p>Camel will resolve variables
according to either:</p><ul class="alternate"><li>namespace given</li><li>no
namespace given</li></ul><h4 id="XPath-NamespaceGiven">Namespace
Given</h4><p>If the namespace is given then Camel is instructed exactly what to
return. However when resolving either <strong><code>IN</code></strong>
or <strong><code>OUT</code></strong> Camel will try to resolve a header
with the given local part first, and return it. If the local part has the
value <strong><code>body</code></strong> then the body is returned
instead.</p><h4 id="XPath-NoNamespac
eGiven">No Namespace Given</h4><p>If there is no namespace given then Camel
resolves only based on the local part. Camel will try to resolve a variable in
the following steps:</p><ul class="alternate"><li>From
<strong><code>variables</code></strong> that has been set using the
<strong><code>variable(name, value)</code></strong> fluent
builder.</li><li>From <strong><code>message.in.header</code></strong> if
there is a header with the given
key.</li><li>From <strong><code>exchange.properties</code></strong> if
there is a property with the given key.</li></ul><h3
id="XPath-Functions">Functions</h3><p>Camel adds the following XPath functions
that can be used to access the exchange:</p><div class="table-wrap"><table
class="confluenceTable"><tbody><tr><th colspan="1" rowspan="1"
class="confluenceTh"><p>Function</p></th><th colspan="1" rowspan="1"
class="confluenceTh"><p>Argument</p></th><th colspan="1" rowspan="1"
class="confluenceTh"><p>Type</p></th><th colspan="1" rowspan="1"
class="confluenceTh"><p>Description</p></th></tr><tr><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>in:body</code></p></td><td
colspan="1" rowspan="1" class="confluenceTd"><p>none</p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>Object</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>Will return
the <strong><code>IN</code></strong> message body.</p></td></tr><tr><td
colspan="1" rowspan="1"
class="confluenceTd"><p><code>in:header</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>the header name</p></td><td colspan="1"
rowspan="1" class="confluenceTd"><code>Object</code></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>Will return
the <strong><code>IN</code></strong> message header.</p></td></tr><tr><td
colspan="1" rowspan="1"
class="confluenceTd"><p><code>out:body</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>none</p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>Objec
t</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p>Will
return the <strong><code>OUT</code></strong> message
body.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>out:header</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>the header name</p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>Object</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>Will return
the <strong><code>OUT</code></strong> message header.</p></td></tr><tr><td
colspan="1" rowspan="1"
class="confluenceTd"><p><code>function:properties</code></p></td><td
colspan="1" rowspan="1" class="confluenceTd"><p>key for property</p></td><td
colspan="1" rowspan="1" class="confluenceTd"><p><code>String</code></p></td><td
colspan="1" rowspan="1" class="confluenceTd"><p><strong>Camel 2.5:</strong> To
lookup a property using the <a shape="rect"
href="properties.html">Properties</a> component (property
placeholders).</p></td></tr
><tr><td colspan="1" rowspan="1"
>class="confluenceTd"><p><code>function:simple</code></p></td><td colspan="1"
>rowspan="1" class="confluenceTd"><p>simple expression</p></td><td colspan="1"
>rowspan="1" class="confluenceTd"><code>Object</code></td><td colspan="1"
>rowspan="1" class="confluenceTd"><p><strong>Camel 2.5:</strong> To evaluate a
><a shape="rect" href="simple.html">Simple</a>
>expression.</p></td></tr></tbody></table></div><div
>class="confluence-information-macro
>confluence-information-macro-information"><span class="aui-icon
>aui-icon-small aui-iconfont-info
>confluence-information-macro-icon"></span><div
>class="confluence-information-macro-body"><strong>Note:
><code>function:properties</code></strong> and
><strong><code>function:simple</code></strong> is not supported when the
>return type is a <strong><code>NodeSet</code></strong>, such as when using
>with a <a shape="rect" href="splitter.html">Splitter</a>
>EIP.</div></div><p>Here's an example showing some of these functions in us
e.</p><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-from("direct:start").choice()
- .when().xpath("in:header('foo') =
'bar'").to("mock:x")
- .when().xpath("in:body() =
'<two/>'").to("mock:y")
- .otherwise().to("mock:z");
-]]></script>
-</div></div>And the new functions introduced in Camel 2.5:<div class="code
panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-// setup properties component
-PropertiesComponent properties = new PropertiesComponent();
-properties.setLocation("classpath:org/apache/camel/builder/xml/myprop.properties");
-context.addComponent("properties", properties);
-
-// myprop.properties contains the following properties
-// foo=Camel
-// bar=Kong
-
-from("direct:in").choice()
- // $type is a variable for the header with key type
- // here we use the properties function to lookup foo from the properties
files
- // which at runtime will be evaluted to 'Camel'
- .when().xpath("$type = function:properties('foo')")
- .to("mock:camel")
- // here we use the simple language to evaluate the expression
- // which at runtime will be evaluated to 'Donkey Kong'
- .when().xpath("//name = function:simple('Donkey
${properties:bar}')")
- .to("mock:donkey")
- .otherwise()
- .to("mock:other")
- .end();
-]]></script>
-</div></div><h3 id="XPath-UsingXMLConfiguration">Using XML
Configuration</h3><p>If you prefer to configure your routes in your <a
shape="rect" href="spring.html">Spring</a> XML file then you can use XPath
expressions as follows</p><div class="code panel pdl" style="border-width:
1px;"><div class="codeContent panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[<beans
xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="
+<div class="wiki-content maincontent"><h2 id="XPath-XPath">XPath</h2><p>Camel
supports <a shape="rect" class="external-link"
href="http://www.w3.org/TR/xpath" rel="nofollow">XPath</a> to allow an <a
shape="rect" href="expression.html">Expression</a> or <a shape="rect"
href="predicate.html">Predicate</a> to be used in the <a shape="rect"
href="dsl.html">DSL</a> or <a shape="rect" href="xml-configuration.html">Xml
Configuration</a>. For example you could use XPath to create an <a shape="rect"
href="predicate.html">Predicate</a> in a <a shape="rect"
href="message-filter.html">Message Filter</a> or as an <a shape="rect"
href="expression.html">Expression</a> for a <a shape="rect"
href="recipient-list.html">Recipient List</a>.</p><parameter
ac:name="title">Streams</parameter><rich-text-body><p>If the message body is
stream based, which means the input is received by Camel as a stream, then you
will only be able to read the content of the stream <strong>once</strong>.
Oftentimes when using
<a shape="rect" href="xpath.html">XPath</a> as <a shape="rect"
href="message-filter.html">Message Filter</a> or <a shape="rect"
href="content-based-router.html">Content Based Router</a> the data will be
accessed multiple times. Therefore use <a shape="rect"
href="stream-caching.html">Stream caching</a> or convert the message body to a
<strong><code>String</code></strong> beforehand. This makes it safe to be
re-read multiple times.</p></rich-text-body><plain-text-body>from("queue:foo")
+ .filter().xpath("//foo"))
+ .to("queue:bar")
+</plain-text-body><plain-text-body>from("queue:foo")
+ .choice().xpath("//foo")).to("queue:bar")
+ .otherwise().to("queue:others");
+</plain-text-body><h3 id="XPath-Namespaces">Namespaces</h3><p>You can easily
use namespaces with XPath expressions using the Namespaces helper
class.<plain-text-body>{snippet:id=example|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/XPathWithNamespacesFilterTest.java}</plain-text-body></p><h3
id="XPath-Variables">Variables</h3><p>Variables in XPath is defined in
different namespaces. The default namespace is <code><a shape="rect"
class="external-link"
href="http://camel.apache.org/schema/spring">http://camel.apache.org/schema/spring</a></code>.</p><div
class="table-wrap"><table class="confluenceTable"><tbody><tr><th colspan="1"
rowspan="1" class="confluenceTh"><p>Namespace URI</p></th><th colspan="1"
rowspan="1" class="confluenceTh"><p>Local part</p></th><th colspan="1"
rowspan="1" class="confluenceTh"><p>Type</p></th><th colspan="1" rowspan="1"
class="confluenceTh"><p>Description</p></th></tr><tr><td colspan="1"
rowspan="1" class="confluenceTd"><a sha
pe="rect" class="external-link"
href="http://camel.apache.org/xml/in/">http://camel.apache.org/xml/in/</a></td><td
colspan="1" rowspan="1" class="confluenceTd"><p><code>in</code></p></td><td
colspan="1" rowspan="1"
class="confluenceTd"><p><code>Message</code></p></td><td colspan="1"
rowspan="1"
class="confluenceTd"><p>The <strong><code>exchange.in</code></strong>
message.</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><a
shape="rect" class="external-link"
href="http://camel.apache.org/xml/out/">http://camel.apache.org/xml/out/</a></td><td
colspan="1" rowspan="1" class="confluenceTd"><p><code>out</code></p></td><td
colspan="1" rowspan="1"
class="confluenceTd"><p><code>Message</code></p></td><td colspan="1"
rowspan="1"
class="confluenceTd"><p>The <strong><code>exchange.out</code></strong>
message.</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><a
shape="rect" class="external-link"
href="http://camel.apache.org/xml/function/">http://camel
.apache.org/xml/function/</a></td><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>functions</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>Object</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><strong>Camel 2.5:</strong> Additional
functions.</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><a
shape="rect" class="external-link"
href="http://camel.apache.org/xml/variables/environment-variables">http://camel.apache.org/xml/variables/environment-variables</a></td><td
colspan="1" rowspan="1" class="confluenceTd"><p><code>env</code></p></td><td
colspan="1" rowspan="1" class="confluenceTd"><code>Object</code></td><td
colspan="1" rowspan="1" class="confluenceTd"><p>OS environment
variables.</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><a
shape="rect" class="external-link"
href="http://camel.apache.org/xml/variables/system-properties">http://camel.apache.org/xml/variables/system-properties</a></td>
<td colspan="1" rowspan="1"
class="confluenceTd"><p><code>system</code></p></td><td colspan="1" rowspan="1"
class="confluenceTd"><code>Object</code></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>Java System properties.</p></td></tr><tr><td
colspan="1" rowspan="1" class="confluenceTd"><a shape="rect"
class="external-link"
href="http://camel.apache.org/xml/variables/exchange-property">http://camel.apache.org/xml/variables/exchange-property</a></td><td
colspan="1" rowspan="1" class="confluenceTd"><p> </p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>Object</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>The exchange
property.</p></td></tr></tbody></table></div><p>Camel will resolve variables
according to either:</p><ul class="alternate"><li>namespace given</li><li>no
namespace given</li></ul><h4 id="XPath-NamespaceGiven">Namespace
Given</h4><p>If the namespace is given then Camel is instructed exactly what to
return. However when resol
ving either <strong><code>IN</code></strong>
or <strong><code>OUT</code></strong> Camel will try to resolve a header
with the given local part first, and return it. If the local part has the
value <strong><code>body</code></strong> then the body is returned
instead.</p><h4 id="XPath-NoNamespaceGiven">No Namespace Given</h4><p>If there
is no namespace given then Camel resolves only based on the local part. Camel
will try to resolve a variable in the following steps:</p><ul
class="alternate"><li>From <strong><code>variables</code></strong> that has
been set using the <strong><code>variable(name, value)</code></strong> fluent
builder.</li><li>From <strong><code>message.in.header</code></strong> if
there is a header with the given
key.</li><li>From <strong><code>exchange.properties</code></strong> if
there is a property with the given key.</li></ul><h3
id="XPath-Functions">Functions</h3><p>Camel adds the following XPath functions
that can be used to access the e
xchange:</p><div class="table-wrap"><table
class="confluenceTable"><tbody><tr><th colspan="1" rowspan="1"
class="confluenceTh"><p>Function</p></th><th colspan="1" rowspan="1"
class="confluenceTh"><p>Argument</p></th><th colspan="1" rowspan="1"
class="confluenceTh"><p>Type</p></th><th colspan="1" rowspan="1"
class="confluenceTh"><p>Description</p></th></tr><tr><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>in:body</code></p></td><td
colspan="1" rowspan="1" class="confluenceTd"><p>none</p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>Object</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>Will return
the <strong><code>IN</code></strong> message body.</p></td></tr><tr><td
colspan="1" rowspan="1"
class="confluenceTd"><p><code>in:header</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>the header name</p></td><td colspan="1"
rowspan="1" class="confluenceTd"><code>Object</code></td><td colspan="1"
rowspan="1" class="confl
uenceTd"><p>Will return the <strong><code>IN</code></strong> message
header.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>out:body</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>none</p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>Object</code></p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>Will return the <strong><code>OUT</code></strong>
message body.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>out:header</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>the header name</p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>Object</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>Will return
the <strong><code>OUT</code></strong> message header.</p></td></tr><tr><td
colspan="1" rowspan="1"
class="confluenceTd"><p><code>function:properties</code></p></td><td
colspan="1" rowspan="1" class="confluenceTd"><p>key for
property</p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>String</code></p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p><strong>Camel 2.5:</strong> To lookup a property using
the <a shape="rect" href="properties.html">Properties</a> component (property
placeholders).</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>function:simple</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>simple expression</p></td><td colspan="1"
rowspan="1" class="confluenceTd"><code>Object</code></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><strong>Camel 2.5:</strong> To evaluate a
<a shape="rect" href="simple.html">Simple</a>
expression.</p></td></tr></tbody></table></div><rich-text-body><strong>Note:
<code>function:properties</code></strong> and
<strong><code>function:simple</code></strong> is not supported when the return
type is a <strong><code>NodeSet</code></strong>, such as when using with a <a
shape="rect" href="spli
tter.html">Splitter</a> EIP.</rich-text-body><p>Here's an example showing some
of these functions in
use.<plain-text-body>{snippet:id=ex|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/language/XPathFunctionTest.java}</plain-text-body>And
the new functions introduced in Camel
2.5:<plain-text-body>{snippet:id=ex|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XPathFunctionsTest.java}</plain-text-body></p><h3
id="XPath-UsingXMLConfiguration">Using XML Configuration</h3><p>If you prefer
to configure your routes in your <a shape="rect" href="spring.html">Spring</a>
XML file then you can use XPath expressions as follows</p><parameter
ac:name="lang">xml</parameter><plain-text-body><beans
xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
- http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd">
+ http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd">
- <camelContext id="camel"
xmlns="http://activemq.apache.org/camel/schema/spring"
xmlns:foo="http://example.com/person">
+ <camelContext id="camel"
xmlns="http://activemq.apache.org/camel/schema/spring"
xmlns:foo="http://example.com/person">
<route>
- <from uri="activemq:MyQueue"/>
+ <from uri="activemq:MyQueue"/>
<filter>
- <xpath>/foo:person[@name='James']</xpath>
- <to uri="mqseries:SomeOtherQueue"/>
+ <xpath>/foo:person[@name='James']</xpath>
+ <to uri="mqseries:SomeOtherQueue"/>
</filter>
</route>
</camelContext>
</beans>
-]]></script>
-</div></div><p>Notice how we can reuse the namespace
prefixes, <strong><code>foo</code></strong> in this case, in the XPath
expression for easier namespace based XPath expressions! See also this <a
shape="rect" class="external-link"
href="http://camel.465427.n5.nabble.com/fail-filter-XPATH-camel-td476424.html"
rel="nofollow">discussion on the mailinglist</a> about using your own
namespaces with XPath.</p><h3 id="XPath-SettingtheResultType">Setting the
Result Type</h3><p>The <a shape="rect" href="xpath.html">XPath</a> expression
will return a result type using native XML objects such as
<strong><code>org.w3c.dom.NodeList</code></strong>. But many times you want a
result type to be a <strong><code>String</code></strong>. To do this you have
to instruct the <a shape="rect" href="xpath.html">XPath</a> which result type
to use.</p><p>In Java DSL:</p><div class="code panel pdl" style="border-width:
1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[xpath("/foo:person/@id",
String.class)
-]]></script>
-</div></div><p>In Spring DSL you use
the <strong><code>resultType</code></strong> attribute to provide a fully
qualified classname:</p><div class="code panel pdl" style="border-width:
1px;"><div class="codeContent panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[<xpath
resultType="java.lang.String">/foo:person/@id</xpath>
-]]></script>
-</div></div><p>In <strong><code>@XPath</code></strong>:<br clear="none">
<strong>Available as of Camel 2.1</strong></p><div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[@XPath(value =
"concat('foo-',//order/name/)", resultType = String.class)
String name)
-]]></script>
-</div></div><p>Where we use the XPath
function <strong><code>concat</code></strong> to prefix the order name
with <strong><code>foo-</code></strong>. In this case we have to specify that
we want a <strong><code>String</code></strong> as result type so
the <strong><code>concat</code></strong> function works.</p><h3
id="XPath-UsingXPathonHeaders">Using XPath on Headers</h3><p><strong>Available
as of Camel 2.11</strong></p><p>Some users may have XML stored in a header. To
apply an XPath statement to a header's value you can do this by defining
the <strong><code>headerName</code></strong> attribute.</p><p>In XML
DSL:</p><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-<camelContext id="xpathHeaderNameTest"
xmlns="http://camel.apache.org/schema/blueprint">
- <route>
- <from uri="direct:in"/>
- <choice>
- <when>
- <!-- use headerName attribute to refer to a header -->
- <xpath headerName="invoiceDetails">/invoice/@orderType
= 'premium'</xpath>
- <to uri="mock:premium"/>
- </when>
- <when>
- <!-- use headerName attribute to refer to a header -->
- <xpath headerName="invoiceDetails">/invoice/@orderType
= 'standard'</xpath>
- <to uri="mock:standard"/>
- </when>
- <otherwise>
- <to uri="mock:unknown"/>
- </otherwise>
- </choice>
- </route>
-</camelContext>
-]]></script>
-</div></div>And in Java DSL you specify
the <strong><code>headerName</code></strong> as the 2nd parameter as
shown:<div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[xpath("/invoice/@orderType =
'premium'", "invoiceDetails")
-]]></script>
-</div></div><h3 id="XPath-Examples">Examples</h3><p>Here is a simple <a
shape="rect" class="external-link"
href="http://svn.apache.org/repos/asf/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/XPathFilterTest.java">example</a>
using an XPath expression as a predicate in a <a shape="rect"
href="message-filter.html">Message Filter</a></p><div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-from("direct:start").
- filter().xpath("/person[@name='James']").
- to("mock:result");
-]]></script>
-</div></div>If you have a standard set of namespaces you wish to work with and
wish to share them across many different XPath expressions you can use
the <strong><code>NamespaceBuilder</code></strong> as shown <a
shape="rect" class="external-link"
href="http://svn.apache.org/repos/asf/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/XPathWithNamespaceBuilderFilterTest.java">in
this example</a><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-// lets define the namespaces we'll need in our filters
-Namespaces ns = new Namespaces("c",
"http://acme.com/cheese")
- .add("xsd", "http://www.w3.org/2001/XMLSchema");
-
-// now lets create an xpath based Message Filter
-from("direct:start").
- filter(ns.xpath("/c:person[@name='James']")).
- to("mock:result");
-]]></script>
-</div></div>In this sample we have a <strong><code>choice</code></strong>
construct. The first choice evaulates if the message has a header
key <strong><code>type</code></strong> that has the value
<strong><code>Camel</code></strong>. The
2nd <strong><code>choice</code></strong> evaluates if the message body has
a name tag <strong><code><name></code></strong> which values is
<strong><code>Kong</code></strong>.<br clear="none"> If neither is true the
message is routed in the otherwise block:<div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-from("direct:in").choice()
- // using $headerName is special notation in Camel to get the header key
- .when().xpath("$type = 'Camel'")
- .to("mock:camel")
- // here we test for the body name tag
- .when().xpath("//name = 'Kong'")
- .to("mock:donkey")
- .otherwise()
- .to("mock:other")
- .end();
-]]></script>
-</div></div>And the spring XML equivalent of the route:<div class="code panel
pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-<camelContext xmlns="http://camel.apache.org/schema/spring">
- <route>
- <from uri="direct:in"/>
- <choice>
- <when>
- <xpath>$type = 'Camel'</xpath>
- <to uri="mock:camel"/>
- </when>
- <when>
- <xpath>//name = 'Kong'</xpath>
- <to uri="mock:donkey"/>
- </when>
- <otherwise>
- <to uri="mock:other"/>
- </otherwise>
- </choice>
- </route>
-</camelContext>
-]]></script>
-</div></div><h2 id="XPath-XPathInjection">XPath Injection</h2><p>You can use
<a shape="rect" href="bean-integration.html">Bean Integration</a> to invoke a
method on a bean and use various languages such as XPath to extract a value
from the message and bind it to a method parameter.</p><p>The default XPath
annotation has SOAP and XML namespaces available. If you want to use your own
namespace URIs in an XPath expression you can use your own copy of the <a
shape="rect" class="external-link"
href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/language/XPath.html">XPath
annotation</a> to create whatever namespace prefixes you want to use.</p><div
class="code panel pdl" style="border-width: 1px;"><div class="codeContent
panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-import java.lang.annotation.ElementType;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import java.lang.annotation.Target;
-
-import org.w3c.dom.NodeList;
-
-import org.apache.camel.component.bean.XPathAnnotationExpressionFactory;
-import org.apache.camel.language.LanguageAnnotation;
-import org.apache.camel.language.NamespacePrefix;
-
-@Retention(RetentionPolicy.RUNTIME)
-@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
-@LanguageAnnotation(language = "xpath", factory =
XPathAnnotationExpressionFactory.class)
-public @interface MyXPath {
- String value();
-
- // You can add the namespaces as the default value of the annotation
- NamespacePrefix[] namespaces() default {
- @NamespacePrefix(prefix = "n1", uri =
"http://example.org/ns1"),
- @NamespacePrefix(prefix = "n2", uri =
"http://example.org/ns2")};
-
- Class<?> resultType() default NodeList.class;
-}
-]]></script>
-</div></div>e.g., cut and paste upper code to your own project in a different
package and/or annotation name then add whatever namespace prefix/URIs you want
in scope when you use your annotation on a method parameter. Then when you use
your annotation on a method parameter all the namespaces you want will be
available for use in your XPath expression.<p>Example:</p><div class="code
panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[public class Foo {
+</plain-text-body><p>Notice how we can reuse the namespace
prefixes, <strong><code>foo</code></strong> in this case, in the XPath
expression for easier namespace based XPath expressions! See also this <a
shape="rect" class="external-link"
href="http://camel.465427.n5.nabble.com/fail-filter-XPATH-camel-td476424.html"
rel="nofollow">discussion on the mailinglist</a> about using your own
namespaces with XPath.</p><h3 id="XPath-SettingtheResultType">Setting the
Result Type</h3><p>The <a shape="rect" href="xpath.html">XPath</a> expression
will return a result type using native XML objects such as
<strong><code>org.w3c.dom.NodeList</code></strong>. But many times you want a
result type to be a <strong><code>String</code></strong>. To do this you have
to instruct the <a shape="rect" href="xpath.html">XPath</a> which result type
to use.</p><p>In Java DSL:</p><parameter
ac:name="language">java</parameter><plain-text-body>xpath("/foo:person/@id",
String.class)
+</plain-text-body><p>In Spring DSL you use
the <strong><code>resultType</code></strong> attribute to provide a fully
qualified classname:</p><parameter
ac:name="">xml</parameter><plain-text-body><xpath
resultType="java.lang.String">/foo:person/@id</xpath>
+</plain-text-body><p>In <strong><code>@XPath</code></strong>:<br clear="none">
<strong>Available as of Camel 2.1</strong></p><parameter
ac:name="language">java</parameter><plain-text-body>@XPath(value =
"concat('foo-',//order/name/)", resultType = String.class) String name)
+</plain-text-body><p>Where we use the XPath
function <strong><code>concat</code></strong> to prefix the order name
with <strong><code>foo-</code></strong>. In this case we have to specify that
we want a <strong><code>String</code></strong> as result type so
the <strong><code>concat</code></strong> function works.</p><h3
id="XPath-UsingXPathonHeaders">Using XPath on Headers</h3><p><strong>Available
as of Camel 2.11</strong></p><p>Some users may have XML stored in a header. To
apply an XPath statement to a header's value you can do this by defining
the <strong><code>headerName</code></strong> attribute.</p><p>In XML
DSL:<plain-text-body>{snippet:id=e1|lang=xml|url=camel/trunk/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/xpath/XPathHeaderNameTest.xml}</plain-text-body>And
in Java DSL you specify the <strong><code>headerName</code></strong> as
the 2nd parameter as shown:</p><parameter
ac:name="language">java</parameter><plai
n-text-body>xpath("/invoice/@orderType = 'premium'", "invoiceDetails")
+</plain-text-body><h3 id="XPath-Examples">Examples</h3><p>Here is a simple <a
shape="rect" class="external-link"
href="http://svn.apache.org/repos/asf/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/XPathFilterTest.java">example</a>
using an XPath expression as a predicate in a <a shape="rect"
href="message-filter.html">Message
Filter</a><plain-text-body>{snippet:id=example|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/XPathFilterTest.java}</plain-text-body>If
you have a standard set of namespaces you wish to work with and wish to share
them across many different XPath expressions you can use
the <strong><code>NamespaceBuilder</code></strong> as shown <a
shape="rect" class="external-link"
href="http://svn.apache.org/repos/asf/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/XPathWithNamespaceBuilderFilterTest.java">in
this
example</a><plain-text-body>{snippet:id=example|lang=java|url=camel/trunk/camel-core/src/test/
java/org/apache/camel/processor/XPathWithNamespaceBuilderFilterTest.java}</plain-text-body>In
this sample we have a <strong><code>choice</code></strong> construct. The
first choice evaulates if the message has a header
key <strong><code>type</code></strong> that has the value
<strong><code>Camel</code></strong>. The
2nd <strong><code>choice</code></strong> evaluates if the message body has
a name tag <strong><code><name></code></strong> which values is
<strong><code>Kong</code></strong>.<br clear="none"> If neither is true the
message is routed in the otherwise
block:<plain-text-body>{snippet:id=e1|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XPathHeaderTest.java}</plain-text-body>And
the spring XML equivalent of the
route:<plain-text-body>{snippet:id=example|lang=xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringXPathHeaderTest-context.xml}</plain-text-body></p><h2
id="
XPath-XPathInjection">XPath Injection</h2><p>You can use <a shape="rect"
href="bean-integration.html">Bean Integration</a> to invoke a method on a bean
and use various languages such as XPath to extract a value from the message and
bind it to a method parameter.</p><p>The default XPath annotation has SOAP and
XML namespaces available. If you want to use your own namespace URIs in an
XPath expression you can use your own copy of the <a shape="rect"
class="external-link"
href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/language/XPath.html">XPath
annotation</a> to create whatever namespace prefixes you want to
use.<plain-text-body>{snippet:id=example|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/MyXPath.java}</plain-text-body>e.g.,
cut and paste upper code to your own project in a different package and/or
annotation name then add whatever namespace prefix/URIs you want in scope when
you use your annotation on a method
parameter. Then when you use your annotation on a method parameter all the
namespaces you want will be available for use in your XPath
expression.</p><p>Example:</p><parameter
ac:name="language">java</parameter><plain-text-body>public class Foo {
- @MessageDriven(uri = "activemq:my.queue")
- public void doSomething(@MyXPath("/ns1:foo/ns2:bar/text()")
String correlationID, @Body String body) {
+ @MessageDriven(uri = "activemq:my.queue")
+ public void doSomething(@MyXPath("/ns1:foo/ns2:bar/text()") String
correlationID, @Body String body) {
// process the inbound message here
}
}
-]]></script>
-</div></div><h3 id="XPath-UsingXPathBuilderWithoutanExchange">Using
XPathBuilder Without an Exchange</h3><p><strong>Available as of Camel
2.3</strong></p><p>You can now use the
<strong><code>org.apache.camel.builder.XPathBuilder</code></strong> without the
need for an <a shape="rect" href="exchange.html">Exchange</a>. This comes handy
if you want to use it as a helper to do custom XPath evaluations. It requires
that you pass in a <a shape="rect" href="camelcontext.html">CamelContext</a>
since a lot of the moving parts inside
the <strong><code>XPathBuilder</code></strong> requires access to the
Camel <a shape="rect" href="type-converter.html">Type Converter</a> and hence
why <a shape="rect" href="camelcontext.html">CamelContext</a> is
needed.</p><p>For example you can do something like this:</p><div class="code
panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[boolean matches =
XPathBuilder.xpath("/foo/bar/@xyz").matches(context,
"<foo><bar xyz='cheese'/></foo>"));
-]]></script>
-</div></div><p>This will match the given predicate.</p><p>You can also
evaluate for example as shown in the following three examples:</p><div
class="code panel pdl" style="border-width: 1px;"><div class="codeContent
panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[String name =
XPathBuilder.xpath("foo/bar").evaluate(context,
"<foo><bar>cheese</bar></foo>", String.class);
-Integer number = XPathBuilder.xpath("foo/bar").evaluate(context,
"<foo><bar>123</bar></foo>", Integer.class);
-Boolean bool = XPathBuilder.xpath("foo/bar").evaluate(context,
"<foo><bar>true</bar></foo>", Boolean.class);
-]]></script>
-</div></div><p>Evaluating with a String result is a common requirement and
thus you can do it a bit simpler:</p><div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[ String name =
XPathBuilder.xpath("foo/bar").evaluate(context,
"<foo><bar>cheese</bar></foo>");
-]]></script>
-</div></div><h3 id="XPath-UsingSaxonwithXPathBuilder">Using Saxon with
XPathBuilder</h3><p><strong>Available as of Camel 2.3</strong></p><p>You need
to add <strong><code>camel-saxon</code></strong> as dependency to your project.
It's now easier to use <a shape="rect" class="external-link"
href="http://saxon.sourceforge.net/" rel="nofollow">Saxon</a> with
the <strong><code>XPathBuilder</code></strong> which can be done in
several ways as shown below. Where as the latter ones are the easiest
ones.</p><p>Using a factory</p><div class="code panel pdl" style="border-width:
1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-// create a Saxon factory
-XPathFactory fac = new net.sf.saxon.xpath.XPathFactoryImpl();
-
-// create a builder to evaluate the xpath using the saxon factory
-XPathBuilder builder = XPathBuilder.xpath("tokenize(/foo/bar,
'_')[2]").factory(fac);
-
-// evaluate as a String result
-String result = builder.evaluate(context,
"<foo><bar>abc_def_ghi</bar></foo>");
-assertEquals("def", result);
-]]></script>
-</div></div>Using the object model<strong><br clear="none"></strong><div
class="code panel pdl" style="border-width: 1px;"><div class="codeContent
panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-// create a builder to evaluate the xpath using saxon based on its object
model uri
-XPathBuilder builder = XPathBuilder.xpath("tokenize(/foo/bar,
'_')[2]").objectModel("http://saxon.sf.net/jaxp/xpath/om");
-
-// evaluate as a String result
-String result = builder.evaluate(context,
"<foo><bar>abc_def_ghi</bar></foo>");
-assertEquals("def", result);
-]]></script>
-</div></div>The easy one<div class="code panel pdl" style="border-width:
1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-// create a builder to evaluate the xpath using saxon
-XPathBuilder builder = XPathBuilder.xpath("tokenize(/foo/bar,
'_')[2]").saxon();
-
-// evaluate as a String result
-String result = builder.evaluate(context,
"<foo><bar>abc_def_ghi</bar></foo>");
-assertEquals("def", result);
-]]></script>
-</div></div><h3
id="XPath-SettingaCustomXPathFactoryUsingSystemProperty">Setting a Custom
XPathFactory Using System Property</h3><p><strong>Available as of Camel
2.3</strong></p><p>Camel now supports reading the <a shape="rect"
class="external-link"
href="http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/xpath/XPathFactory.html#newInstance(java.lang.String)"
rel="nofollow">JVM system property
<code>javax.xml.xpath.XPathFactory</code></a> that can be used to set a
custom <strong><code>XPathFactory</code></strong> to use.</p><p>This unit
test shows how this can be done to use Saxon instead:</p><div class="code panel
pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-// set system property with the XPath factory to use which is Saxon
-System.setProperty(XPathFactory.DEFAULT_PROPERTY_NAME + ":" +
"http://saxon.sf.net/jaxp/xpath/om",
"net.sf.saxon.xpath.XPathFactoryImpl");
-
-// create a builder to evaluate the xpath using saxon
-XPathBuilder builder = XPathBuilder.xpath("tokenize(/foo/bar,
'_')[2]");
-
-// evaluate as a String result
-String result = builder.evaluate(context,
"<foo><bar>abc_def_ghi</bar></foo>");
-assertEquals("def", result);
-]]></script>
-</div></div>Camel will log at <strong><code>INFO</code></strong> level if it
uses a non default <strong><code>XPathFactory</code></strong> such as:<div
class="code panel pdl" style="border-width: 1px;"><div class="codeContent
panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[XPathBuilder INFO Using system property
javax.xml.xpath.XPathFactory:http://saxon.sf.net/jaxp/xpath/om with value:
+</plain-text-body><h3 id="XPath-UsingXPathBuilderWithoutanExchange">Using
XPathBuilder Without an Exchange</h3><p><strong>Available as of Camel
2.3</strong></p><p>You can now use the
<strong><code>org.apache.camel.builder.XPathBuilder</code></strong> without the
need for an <a shape="rect" href="exchange.html">Exchange</a>. This comes handy
if you want to use it as a helper to do custom XPath evaluations. It requires
that you pass in a <a shape="rect" href="camelcontext.html">CamelContext</a>
since a lot of the moving parts inside
the <strong><code>XPathBuilder</code></strong> requires access to the
Camel <a shape="rect" href="type-converter.html">Type Converter</a> and hence
why <a shape="rect" href="camelcontext.html">CamelContext</a> is
needed.</p><p>For example you can do something like this:</p><parameter
ac:name="language">java</parameter><plain-text-body>boolean matches =
XPathBuilder.xpath("/foo/bar/@xyz").matches(context, "<foo><bar
xyz='cheese'/></foo&g
t;"));
+</plain-text-body><p>This will match the given predicate.</p><p>You can also
evaluate for example as shown in the following three examples:</p><parameter
ac:name="language">java</parameter><plain-text-body>String name =
XPathBuilder.xpath("foo/bar").evaluate(context,
"<foo><bar>cheese</bar></foo>", String.class);
+Integer number = XPathBuilder.xpath("foo/bar").evaluate(context,
"<foo><bar>123</bar></foo>", Integer.class);
+Boolean bool = XPathBuilder.xpath("foo/bar").evaluate(context,
"<foo><bar>true</bar></foo>", Boolean.class);
+</plain-text-body><p>Evaluating with a String result is a common requirement
and thus you can do it a bit simpler:</p><plain-text-body> String name =
XPathBuilder.xpath("foo/bar").evaluate(context,
"<foo><bar>cheese</bar></foo>");
+</plain-text-body><h3 id="XPath-UsingSaxonwithXPathBuilder">Using Saxon with
XPathBuilder</h3><p><strong>Available as of Camel 2.3</strong></p><p>You need
to add <strong><code>camel-saxon</code></strong> as dependency to your project.
It's now easier to use <a shape="rect" class="external-link"
href="http://saxon.sourceforge.net/" rel="nofollow">Saxon</a> with
the <strong><code>XPathBuilder</code></strong> which can be done in
several ways as shown below. Where as the latter ones are the easiest
ones.</p><p>Using a
factory<plain-text-body>{snippet:id=e1|lang=java|url=camel/trunk/components/camel-saxon/src/test/java/org/apache/camel/builder/saxon/XPathTest.java}</plain-text-body>Using
the object model<strong><br
clear="none"></strong><plain-text-body>{snippet:id=e2|lang=java|url=camel/trunk/components/camel-saxon/src/test/java/org/apache/camel/builder/saxon/XPathTest.java}</plain-text-body>The
easy
one<plain-text-body>{snippet:id=e3|lang=java|url=camel/trunk/components/camel-sax
on/src/test/java/org/apache/camel/builder/saxon/XPathTest.java}</plain-text-body></p><h3
id="XPath-SettingaCustomXPathFactoryUsingSystemProperty">Setting a Custom
XPathFactory Using System Property</h3><p><strong>Available as of Camel
2.3</strong></p><p>Camel now supports reading the <a shape="rect"
class="external-link"
href="http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/xpath/XPathFactory.html#newInstance(java.lang.String)"
rel="nofollow">JVM system property
<code>javax.xml.xpath.XPathFactory</code></a> that can be used to set a
custom <strong><code>XPathFactory</code></strong> to use.</p><p>This unit
test shows how this can be done to use Saxon
instead:<plain-text-body>{snippet:id=e4|lang=java|url=camel/trunk/components/camel-saxon/src/test/java/org/apache/camel/builder/saxon/XPathTest.java}</plain-text-body>Camel
will log at <strong><code>INFO</code></strong> level if it uses a non
default <strong><code>XPathFactory</code></strong> such
as:</p><plain-text-body>XPat
hBuilder INFO Using system property
javax.xml.xpath.XPathFactory:http://saxon.sf.net/jaxp/xpath/om with value:
net.sf.saxon.xpath.XPathFactoryImpl when creating
XPathFactory
-]]></script>
-</div></div><p>To use Apache Xerces you can configure the system
property:</p><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[-Djavax.xml.xpath.XPathFactory=org.apache.xpath.jaxp.XPathFactoryImpl
-]]></script>
-</div></div><h3 id="XPath-EnablingSaxonfromSpringDSL">Enabling Saxon from
Spring DSL</h3><p><strong>Available as of Camel 2.10</strong></p><p>Similarly
to Java DSL, to enable Saxon from Spring DSL you have three
options:</p><p>Specifying the factory</p><div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[<xpath factoryRef="saxonFactory"
resultType="java.lang.String">current-dateTime()</xpath>
-]]></script>
-</div></div><p>Specifying the object model</p><div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[<xpath
objectModel="http://saxon.sf.net/jaxp/xpath/om"
resultType="java.lang.String">current-dateTime()</xpath>
-]]></script>
-</div></div><p>Shortcut</p><div class="code panel pdl" style="border-width:
1px;"><div class="codeContent panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[<xpath saxon="true"
resultType="java.lang.String">current-dateTime()</xpath>
-]]></script>
-</div></div><h3 id="XPath-NamespaceAuditingtoAidDebugging">Namespace Auditing
to Aid Debugging</h3><p><strong>Available as of Camel 2.10</strong></p><p>A
large number of XPath-related issues that users frequently face are linked to
the usage of namespaces. You may have some misalignment between the namespaces
present in your message and those that your XPath expression is aware of or
referencing. XPath predicates or expressions that are unable to locate the XML
elements and attributes due to namespaces issues may simply look like "they are
not working", when in reality all there is to it is a lack of namespace
definition.</p><p>Namespaces in XML are completely necessary, and while we
would love to simplify their usage by implementing some magic or voodoo to wire
namespaces automatically, truth is that any action down this path would
disagree with the standards and would greatly hinder
interoperability.</p><p>Therefore, the utmost we can do is assist you in
debugging such issues by a
dding two new features to the XPath Expression Language and are thus
accessible from both predicates and expressions.</p><h4
id="XPath-LoggingtheNamespaceContextofYourXPathExpression/Predicate">Logging
the Namespace Context of Your XPath Expression/Predicate</h4><p>Every time a
new XPath expression is created in the internal pool, Camel will log the
namespace context of the expression under the
<strong><code>org.apache.camel.builder.xml.XPathBuilder</code></strong> logger.
Since Camel represents Namespace Contexts in a hierarchical fashion
(parent-child relationships), the entire tree is output in a recursive manner
with the following format:</p><div class="code panel pdl" style="border-width:
1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[[me: {prefix -> namespace}, {prefix ->
namespace}], [parent: [me: {prefix -> namespace}, {prefix -> namespace}],
[parent: [me: {prefix -> namespace}]]]
-]]></script>
-</div></div><p>Any of these options can be used to activate this
logging:</p><ol><li>Enable <strong><code>TRACE</code></strong> logging on
the <strong><code>org.apache.camel.builder.xml.XPathBuilder</code></strong>
logger, or some parent logger such as
<strong><code>org.apache.camel</code></strong> or the root
logger.</li><li>Enable the <strong><code>logNamespaces</code></strong> option
as indicated in <a shape="rect" href="#XPath-AuditingNamespaces">Auditing
Namespaces</a>, in which case the logging will occur on
the <strong><code>INFO</code></strong> level.</li></ol><p><span
class="confluence-anchor-link" id="XPath-AuditingNamespaces"></span></p><h4
id="XPath-Auditingnamespaces">Auditing namespaces</h4><p>Camel is able to
discover and dump all namespaces present on every incoming message before
evaluating an XPath expression, providing all the richness of information you
need to help you analyse and pinpoint possible namespace issues. To achieve
this, it in turn internal
ly uses another specially tailored XPath expression to extract all namespace
mappings that appear in the message, displaying the prefix and the full
namespace URI(s) for each individual mapping.</p><p>Some points to take into
account:</p><ul><li>The implicit XML namespace
(xmlns:xml="http://www.w3.org/XML/1998/namespace") is suppressed from the
output because it adds no value.</li><li>Default namespaces are listed under
the <strong><code>DEFAULT</code></strong> keyword in the
output.</li><li>Keep in mind that namespaces can be remapped under different
scopes. Think of a top-level 'a' prefix which in inner elements can be assigned
a different namespace, or the default namespace changing in inner scopes. For
each discovered prefix, all associated URIs are listed.</li></ul><p>You can
enable this option in Java DSL and Spring DSL.</p><p>Java DSL:</p><div
class="code panel pdl" style="border-width: 1px;"><div class="codeContent
panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[XPathBuilder.xpath("/foo:person/@id",
String.class).logNamespaces()
-]]></script>
-</div></div><p>Spring DSL:</p><div class="code panel pdl" style="border-width:
1px;"><div class="codeContent panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[<xpath logNamespaces="true"
resultType="String">/foo:person/@id</xpath>
-]]></script>
-</div></div><p>The result of the auditing will be appear at
the <strong><code>INFO</code></strong> level under the
<strong><code>org.apache.camel.builder.xml.XPathBuilder</code></strong> logger
and will look like the following:</p><div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[2012-01-16 13:23:45,878 [stSaxonWithFlag]
INFO XPathBuilder - Namespaces discovered in message:
+</plain-text-body><p>To use Apache Xerces you can configure the system
property:</p><plain-text-body>-Djavax.xml.xpath.XPathFactory=org.apache.xpath.jaxp.XPathFactoryImpl
+</plain-text-body><h3 id="XPath-EnablingSaxonfromSpringDSL">Enabling Saxon
from Spring DSL</h3><p><strong>Available as of Camel
2.10</strong></p><p>Similarly to Java DSL, to enable Saxon from Spring DSL you
have three options:</p><p>Specifying the factory</p><parameter
ac:name="">xml</parameter><plain-text-body><xpath factoryRef="saxonFactory"
resultType="java.lang.String">current-dateTime()</xpath>
+</plain-text-body><p>Specifying the object model</p><parameter
ac:name="">xml</parameter><plain-text-body><xpath
objectModel="http://saxon.sf.net/jaxp/xpath/om"
resultType="java.lang.String">current-dateTime()</xpath>
+</plain-text-body><p>Shortcut</p><parameter
ac:name="">xml</parameter><plain-text-body><xpath saxon="true"
resultType="java.lang.String">current-dateTime()</xpath>
+</plain-text-body><h3 id="XPath-NamespaceAuditingtoAidDebugging">Namespace
Auditing to Aid Debugging</h3><p><strong>Available as of Camel
2.10</strong></p><p>A large number of XPath-related issues that users
frequently face are linked to the usage of namespaces. You may have some
misalignment between the namespaces present in your message and those that your
XPath expression is aware of or referencing. XPath predicates or expressions
that are unable to locate the XML elements and attributes due to namespaces
issues may simply look like "they are not working", when in reality all there
is to it is a lack of namespace definition.</p><p>Namespaces in XML are
completely necessary, and while we would love to simplify their usage by
implementing some magic or voodoo to wire namespaces automatically, truth is
that any action down this path would disagree with the standards and would
greatly hinder interoperability.</p><p>Therefore, the utmost we can do is
assist you in debugging such issue
s by adding two new features to the XPath Expression Language and are thus
accessible from both predicates and expressions.</p><h4
id="XPath-LoggingtheNamespaceContextofYourXPathExpression/Predicate">Logging
the Namespace Context of Your XPath Expression/Predicate</h4><p>Every time a
new XPath expression is created in the internal pool, Camel will log the
namespace context of the expression under the
<strong><code>org.apache.camel.builder.xml.XPathBuilder</code></strong> logger.
Since Camel represents Namespace Contexts in a hierarchical fashion
(parent-child relationships), the entire tree is output in a recursive manner
with the following format:</p><plain-text-body>[me: {prefix -> namespace},
{prefix -> namespace}], [parent: [me: {prefix -> namespace}, {prefix
-> namespace}], [parent: [me: {prefix -> namespace}]]]
+</plain-text-body><p>Any of these options can be used to activate this
logging:</p><ol><li>Enable <strong><code>TRACE</code></strong> logging on
the <strong><code>org.apache.camel.builder.xml.XPathBuilder</code></strong>
logger, or some parent logger such as
<strong><code>org.apache.camel</code></strong> or the root
logger.</li><li>Enable the <strong><code>logNamespaces</code></strong> option
as indicated in <a shape="rect" href="#XPath-AuditingNamespaces">Auditing
Namespaces</a>, in which case the logging will occur on
the <strong><code>INFO</code></strong> level.</li></ol><p><parameter
ac:name="">AuditingNamespaces</parameter></p><h4
id="XPath-Auditingnamespaces">Auditing namespaces</h4><p>Camel is able to
discover and dump all namespaces present on every incoming message before
evaluating an XPath expression, providing all the richness of information you
need to help you analyse and pinpoint possible namespace issues. To achieve
this, it in turn internally uses another
specially tailored XPath expression to extract all namespace mappings that
appear in the message, displaying the prefix and the full namespace URI(s) for
each individual mapping.</p><p>Some points to take into account:</p><ul><li>The
implicit XML namespace (xmlns:xml="http://www.w3.org/XML/1998/namespace") is
suppressed from the output because it adds no value.</li><li>Default namespaces
are listed under the <strong><code>DEFAULT</code></strong> keyword in the
output.</li><li>Keep in mind that namespaces can be remapped under different
scopes. Think of a top-level 'a' prefix which in inner elements can be assigned
a different namespace, or the default namespace changing in inner scopes. For
each discovered prefix, all associated URIs are listed.</li></ul><p>You can
enable this option in Java DSL and Spring DSL.</p><p>Java DSL:</p><parameter
ac:name="">java</parameter><plain-text-body>XPathBuilder.xpath("/foo:person/@id",
String.class).logNamespaces()
+</plain-text-body><p>Spring DSL:</p><parameter
ac:name="">xml</parameter><plain-text-body><xpath logNamespaces="true"
resultType="String">/foo:person/@id</xpath>
+</plain-text-body><p>The result of the auditing will be appear at
the <strong><code>INFO</code></strong> level under the
<strong><code>org.apache.camel.builder.xml.XPathBuilder</code></strong> logger
and will look like the following:</p><plain-text-body>2012-01-16 13:23:45,878
[stSaxonWithFlag] INFO XPathBuilder - Namespaces discovered in message:
{xmlns:a=[http://apache.org/camel], DEFAULT=[http://apache.org/default],
xmlns:b=[http://apache.org/camelA, http://apache.org/camelB]}
-]]></script>
-</div></div><h3 id="XPath-LoadingScriptfromExternalResource">Loading Script
from External Resource</h3><p><strong>Available as of Camel
2.11</strong></p><p>You can externalize the script and have Camel load it from
a resource such as: <strong><code>classpath:</code></strong>,
<code><strong>file:</strong></code> or <code><strong>http:</strong></code>.<br
clear="none"> This is done using the following syntax:
<code><strong>resource:scheme:location</strong></code>, e.g., to refer to a
file on the classpath you can do:</p><div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[.setHeader("myHeader").xpath("resource:classpath:myxpath.txt",
String.class)
-]]></script>
-</div></div><h3 id="XPath-Dependencies">Dependencies</h3><p>The XPath language
is part of camel-core.</p></div>
+</plain-text-body><h3 id="XPath-LoadingScriptfromExternalResource">Loading
Script from External Resource</h3><p><strong>Available as of Camel
2.11</strong></p><p>You can externalize the script and have Camel load it from
a resource such as: <strong><code>classpath:</code></strong>,
<code><strong>file:</strong></code> or <code><strong>http:</strong></code>.<br
clear="none"> This is done using the following syntax:
<code><strong>resource:scheme:location</strong></code>, e.g., to refer to a
file on the classpath you can
do:</p><plain-text-body>.setHeader("myHeader").xpath("resource:classpath:myxpath.txt",
String.class)
+</plain-text-body><h3 id="XPath-Dependencies">Dependencies</h3><p>The XPath
language is part of camel-core.</p></div>
</td>
<td valign="top">
<div class="navigation">
Modified: websites/production/camel/content/xstream.html
==============================================================================
--- websites/production/camel/content/xstream.html (original)
+++ websites/production/camel/content/xstream.html Fri Aug 25 08:22:01 2017
@@ -36,17 +36,6 @@
<![endif]-->
- <link href='//camel.apache.org/styles/highlighter/styles/shCoreCamel.css'
rel='stylesheet' type='text/css' />
- <link href='//camel.apache.org/styles/highlighter/styles/shThemeCamel.css'
rel='stylesheet' type='text/css' />
- <script src='//camel.apache.org/styles/highlighter/scripts/shCore.js'
type='text/javascript'></script>
- <script src='//camel.apache.org/styles/highlighter/scripts/shBrushJava.js'
type='text/javascript'></script>
- <script src='//camel.apache.org/styles/highlighter/scripts/shBrushXml.js'
type='text/javascript'></script>
- <script src='//camel.apache.org/styles/highlighter/scripts/shBrushPlain.js'
type='text/javascript'></script>
-
- <script type="text/javascript">
- SyntaxHighlighter.defaults['toolbar'] = false;
- SyntaxHighlighter.all();
- </script>
<title>
Apache Camel: XStream
@@ -86,69 +75,35 @@
<tbody>
<tr>
<td valign="top" width="100%">
-<div class="wiki-content maincontent"><h2
id="XStream-XStream">XStream</h2><p>XStream is a <a shape="rect"
href="data-format.html">Data Format</a> which uses the <a shape="rect"
class="external-link" href="http://xstream.codehaus.org/"
rel="nofollow">XStream library</a> to marshal and unmarshal Java objects to and
from XML.</p><p>To use XStream in your camel routes you need to add the a
dependency on <strong>camel-xstream</strong> which implements this
data format.</p><p>Maven users will need to add the following dependency to
their <code>pom.xml</code> for this component:</p><div class="code panel
pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[<dependency>
+<div class="wiki-content maincontent"><h2
id="XStream-XStream">XStream</h2><p>XStream is a <a shape="rect"
href="data-format.html">Data Format</a> which uses the <a shape="rect"
class="external-link" href="http://xstream.codehaus.org/"
rel="nofollow">XStream library</a> to marshal and unmarshal Java objects to and
from XML.</p><p>To use XStream in your camel routes you need to add the a
dependency on <strong>camel-xstream</strong> which implements this
data format.</p><p>Maven users will need to add the following dependency to
their <code>pom.xml</code> for this component:</p><parameter
ac:name="language">xml</parameter><plain-text-body><dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-xstream</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency>
-]]></script>
-</div></div><h3 id="XStream-UsingtheJavaDSL">Using the Java DSL</h3><div
class="code panel pdl" style="border-width: 1px;"><div class="codeContent
panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[// lets turn Object messages into XML then
send to MQSeries
-from("activemq:My.Queue").
+</plain-text-body><h3 id="XStream-UsingtheJavaDSL">Using the Java
DSL</h3><plain-text-body>// lets turn Object messages into XML then send to
MQSeries
+from("activemq:My.Queue").
marshal().xstream().
- to("mqseries:Another.Queue");
-]]></script>
-</div></div><p>If you would like to configure the <code>XStream</code>
instance used by the Camel for the message transformation, you can simply pass
a reference to that instance on the DSL level.</p><div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[XStream xStream = new XStream();
-xStream.aliasField("money", PurchaseOrder.class, "cash");
+ to("mqseries:Another.Queue");
+</plain-text-body><p>If you would like to configure the <code>XStream</code>
instance used by the Camel for the message transformation, you can simply pass
a reference to that instance on the DSL level.</p><plain-text-body>XStream
xStream = new XStream();
+xStream.aliasField("money", PurchaseOrder.class, "cash");
// new Added setModel option since Camel 2.14
-xStream.setModel("NO_REFERENCES");
+xStream.setModel("NO_REFERENCES");
...
-from("direct:marshal").
+from("direct:marshal").
marshal(new XStreamDataFormat(xStream)).
- to("mock:marshaled");
-]]></script>
-</div></div><h3
id="XStream-XMLInputFactoryandXMLOutputFactory">XMLInputFactory and
XMLOutputFactory</h3><p><a shape="rect" class="external-link"
href="http://xstream.codehaus.org/" rel="nofollow">The XStream library</a> uses
the <code>javax.xml.stream.XMLInputFactory</code> and
<code>javax.xml.stream.XMLOutputFactory</code>, you can control which
implementation of this factory should be used.</p><p>The Factory is discovered
using this algorithm:<br clear="none"> 1. Use the
<code>javax.xml.stream.XMLInputFactory</code> ,
<code>javax.xml.stream.XMLOutputFactory</code> system property.<br
clear="none"> 2. Use the <code>lib/xml.stream.properties</code> file in the
<code>JRE_HOME</code> directory.<br clear="none"> 3. Use the Services API, if
available, to determine the classname by looking in the
<code>META-INF/services/javax.xml.stream.XMLInputFactory</code>,
<code>META-INF/services/javax.xml.stream.XMLOutputFactory</code> files in jars
available to the JRE.<br clear="none"> 4. Use the
platform default XMLInputFactory,XMLOutputFactory instance.</p><h3
id="XStream-HowtosettheXMLencodinginXstreamDataFormat?">How to set the XML
encoding in Xstream DataFormat?</h3><p>From Camel 2.2.0, you can set the
encoding of XML in Xstream DataFormat by setting the Exchange's property with
the key <code>Exchange.CHARSET_NAME</code>, or setting the encoding property on
Xstream from DSL or Spring config.</p><div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[from("activemq:My.Queue").
- marshal().xstream("UTF-8").
- to("mqseries:Another.Queue");
-]]></script>
-</div></div><p></p><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-<camelContext id="camel"
xmlns="http://camel.apache.org/schema/spring">
-
- <!-- we define the json xstream data formats to be used (xstream is
default) -->
- <dataFormats>
- <xstream id="xstream-utf8" encoding="UTF-8"
permissions="org.apache.camel.dataformat.stream.*"/>
- <xstream id="xstream-default"
permissions="org.apache.camel.dataformat.stream.*"/>
- </dataFormats>
-
- <route>
- <from uri="direct:in"/>
- <marshal ref="xstream-default"/>
- <to uri="mock:result"/>
- </route>
-
- <route>
- <from uri="direct:in-UTF-8"/>
- <marshal ref="xstream-utf8"/>
- <to uri="mock:result"/>
- </route>
-
-</camelContext>
-]]></script>
-</div></div><h3
id="XStream-SettingthetypepermissionsofXstreamDataFormat">Setting the type
permissions of Xstream DataFormat</h3><p>In Camel, one can always use its own
processing step in the route to filter and block certain XML documents to be
routed to the XStream's unmarhall step. From Camel 2.16.1, 2.15.5, you can
set <a shape="rect" class="external-link"
href="http://x-stream.github.io/security.html" rel="nofollow">XStream's type
permissions</a> to automatically allow or deny the instantiation of
certain types.</p><p>The default type permissions setting used by Camel denies
all types except for those from java.lang and java.util packages. This setting
can be changed by setting System property org.apache.camel.xstream.permissions.
Its value is a string of comma-separated permission terms, each representing a
type being allowed or denied, depending on whether the term is prefixed with
'+' (note '+' may be omitted) or with '-', respectively.</p><p>Each term may
contain
a wildcard character '*'. For example, value "-*,java.lang.*,java.util.*"
indicates denying all types except for java.lang.* and java.util.* classes.
Setting this value to an empty string "" reverts to the default XStream's type
permissions handling which denies certain blacklisted classes and allow
others.</p><p>The type permissions setting can be extended at an individual
XStream DataFormat instance by setting its type permissions property.</p><div
class="code panel pdl" style="border-width: 1px;"><div class="codeContent
panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[ <dataFormats>
- <xstream id="xstream-default"
-
permissions="org.apache.camel.samples.xstream.*"/>
+ to("mock:marshaled");
+</plain-text-body><h3
id="XStream-XMLInputFactoryandXMLOutputFactory">XMLInputFactory and
XMLOutputFactory</h3><p><a shape="rect" class="external-link"
href="http://xstream.codehaus.org/" rel="nofollow">The XStream library</a> uses
the <code>javax.xml.stream.XMLInputFactory</code> and
<code>javax.xml.stream.XMLOutputFactory</code>, you can control which
implementation of this factory should be used.</p><p>The Factory is discovered
using this algorithm:<br clear="none"> 1. Use the
<code>javax.xml.stream.XMLInputFactory</code> ,
<code>javax.xml.stream.XMLOutputFactory</code> system property.<br
clear="none"> 2. Use the <code>lib/xml.stream.properties</code> file in the
<code>JRE_HOME</code> directory.<br clear="none"> 3. Use the Services API, if
available, to determine the classname by looking in the
<code>META-INF/services/javax.xml.stream.XMLInputFactory</code>,
<code>META-INF/services/javax.xml.stream.XMLOutputFactory</code> files in jars
available to the JRE.<br clear="none"> 4. U
se the platform default XMLInputFactory,XMLOutputFactory instance.</p><h3
id="XStream-HowtosettheXMLencodinginXstreamDataFormat?">How to set the XML
encoding in Xstream DataFormat?</h3><p>From Camel 2.2.0, you can set the
encoding of XML in Xstream DataFormat by setting the Exchange's property with
the key <code>Exchange.CHARSET_NAME</code>, or setting the encoding property on
Xstream from DSL or Spring
config.</p><plain-text-body>from("activemq:My.Queue").
+ marshal().xstream("UTF-8").
+ to("mqseries:Another.Queue");
+</plain-text-body><p><plain-text-body>{snippet:id=e1|lang=xml|url=camel/trunk/components/camel-xstream/src/test/resources/org/apache/camel/dataformat/xstream/SpringMarshalListTest.xml}</plain-text-body></p><h3
id="XStream-SettingthetypepermissionsofXstreamDataFormat">Setting the type
permissions of Xstream DataFormat</h3><p>In Camel, one can always use its own
processing step in the route to filter and block certain XML documents to be
routed to the XStream's unmarhall step. From Camel 2.16.1, 2.15.5, you can
set <a shape="rect" class="external-link"
href="http://x-stream.github.io/security.html" rel="nofollow">XStream's type
permissions</a> to automatically allow or deny the instantiation of
certain types.</p><p>The default type permissions setting used by Camel denies
all types except for those from java.lang and java.util packages. This setting
can be changed by setting System property org.apache.camel.xstream.permissions.
Its value is a string of comma-separated permis
sion terms, each representing a type being allowed or denied, depending on
whether the term is prefixed with '+' (note '+' may be omitted) or with '-',
respectively.</p><p>Each term may contain a wildcard character '*'. For
example, value "-*,java.lang.*,java.util.*" indicates denying all types except
for java.lang.* and java.util.* classes. Setting this value to an empty string
"" reverts to the default XStream's type permissions handling which denies
certain blacklisted classes and allow others.</p><p>The type permissions
setting can be extended at an individual XStream DataFormat instance by setting
its type permissions property.</p><plain-text-body> <dataFormats>
+ <xstream id="xstream-default"
+ permissions="org.apache.camel.samples.xstream.*"/>
...
-]]></script>
-</div></div></div>
+</plain-text-body></div>
</td>
<td valign="top">
<div class="navigation">