I'm guessing that you expected to receive the 'ALLAH' string at your
endpoint and were receiving a DeferredCDATASectionImpl object instead is
this correct? The unit test below illustrates what is happening (it uses the
java DSL with the first of the 3 routes being pretty much the same as what
your spring xml route is defining).
The test 'testFailsDueToTypeConversion' shows that what you actually receive
from the xpath split is a instance of DeferredCDATASectionImpl. I'm guessing
you expected this to be a String and were trying to use value() to make it
so. There are a couple of ways to do this as the rest of the test shows.
First and easiest is to use a result type in your xpath expression, e.g.
xpath("/request/header/source/text()", String.class), this will correctly
extract the data from the DeferredCDATASectionImpl object. Unfortunately,
you cannot currently specify a result type in an XML route, though It would
be an easy thing to add so maybe you should open a Jira ticket.
Secondly you could use an intermediate bean to grab the data from the
DeferredCDATASectionImpl and pass it on e.g. 'testWithBeanConversion.'
Third and probably the best would be to add an explicit type converter for
it. The test 'testWithRegisteredConverter' shows how using the
CDataTypeConverter bean below. There is excellent documentation on Type
converters at the Camel website http://camel.apache.org/type-converter.html
Hope this helps,
ste
public class CDataXPathTest extends ContextTestSupport
{
@Override
protected RouteBuilder createRouteBuilder() throws Exception
{
return new RouteBuilder()
{
@Override
public void configure() throws Exception
{
from("direct:xpath-no-conversion").split().xpath("/request/header/source/text()").to("mock:test");
from("direct:xpath-with-result-type").split().xpath("/request/header/source/text()",
String.class).to("mock:test");
from("direct:xpath-with-bean-converter").split().xpath("/request/header/source/text()").bean(new
MySourceProcessor()).to("mock:test");
}
};
}
public void testFailsDueToTypeConversion() throws Exception
{
MockEndpoint mock = getMockEndpoint("mock:test");
mock.expectedMessageCount(1);
String xml =
"<request><header><source><![CDATA[ALLAH]]></source></header></request>";
sendBody("direct:xpath-no-conversion", xml);
mock.await();
mock.assertIsSatisfied();
Object receieved =
ExchangeHelper.getMandatoryInBody(mock.getReceivedExchanges().get(0));
assertIsInstanceOf(DeferredCDATASectionImpl.class, receieved);
}
public void testWithResultTypeConversion() throws Exception
{
MockEndpoint mock = getMockEndpoint("mock:test");
mock.expectedBodiesReceived("ALLAH");
String xml =
"<request><header><source><![CDATA[ALLAH]]></source></header></request>";
sendBody("direct:xpath-with-result-type", xml);
mock.await();
mock.assertIsSatisfied();
}
public void testWithBeanConversion() throws Exception
{
MockEndpoint mock = getMockEndpoint("mock:test");
mock.expectedBodiesReceived("ALLAH");
String xml =
"<request><header><source><![CDATA[ALLAH]]></source></header></request>";
sendBody("direct:xpath-with-bean-converter", xml);
mock.await();
mock.assertIsSatisfied();
}
public void testWithRegisteredConverter() throws Exception
{
context.getTypeConverterRegistry().addTypeConverter(String.class,
DeferredCDATASectionImpl.class, new CDataTypeConverter());
MockEndpoint mock = getMockEndpoint("mock:test");
mock.expectedBodiesReceived("ALLAH");
String xml =
"<request><header><source><![CDATA[ALLAH]]></source></header></request>";
sendBody("direct:xpath-no-conversion", xml);
mock.await();
mock.assertIsSatisfied();
}
public static class MySourceProcessor
{
public String handleCdata(DeferredCDATASectionImpl payload)
{
return payload.getData();
}
}
public static class CDataTypeConverter implements TypeConverter
{
public <T> T convertTo(Class<T> type, Object value) {
DeferredCDATASectionImpl cdata =
(DeferredCDATASectionImpl)value;
return (T) cdata.getData();
}
public <T> T convertTo(Class<T> type, Exchange exchange, Object
value) {
return convertTo(type, value);
}
public <T> T mandatoryConvertTo(Class<T> type, Object value) {
return convertTo(type, value);
}
public <T> T mandatoryConvertTo(Class<T> type, Exchange exchange,
Object value) {
return convertTo(type, value);
}
}
}
On Wed, Dec 16, 2009 at 9:27 AM, titexe <[email protected]> wrote:
>
> hello,
>
> my xpath query does not work, when I put the text value () to finish:
>
> /request/header/source/text()
>
> body of my message :
>
> <?xml version="1.0" encoding="UTF-8"?>
> <request>
> <header>
> <source><![CDATA[ALLAH]]></source>
> </header>
> </request>
>
>
> my route :
>
> <route>
> <from uri="activemq:queue:IN1"/>
> <split>
> <xpath>/request/header/source/text()</ xpath>
>
> <t uri="activemq:queue:XML"/>
> </ Split>
> <t uri="activemq:queue:RESULT"/>
> </ route>
>
> any idea?
>
> thanks in advance,
>
> titexe
> --
> View this message in context:
> http://old.nabble.com/camel-%3A-xpath---text%28%29-tp26815065p26815065.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
>