camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Neil Thorne <>
Subject Problem with XPath splitters
Date Fri, 31 Aug 2007 00:22:32 GMT

Hi I've got a test case here where I can't figure out how to use the
XPathBuilder to create Splitter Expressions.

I've got two tests in here. One suceeds. That's my custom splitter. It's all
pure Java and suits my needs but I just spent so long on this XPath version
it's just bugging me now.

You can see my expectations in the test but in summary - 

For an xml snippet:

&lt;a&gt;&lt;b&gt;Ginger&lt;/b&gt;&lt;b&gt;Mr Boots&lt;/b&gt;&lt;/a&gt;

If I apply the xpath expression "a/b" in a splitter I expect to get a list
of two Strings

&lt;b&gt;Mr Boots&lt;/b&gt;

but I only ever see the String 'Ginger' arrive at the destination endpoint.

What am I going wrong? Does the XPath expression just dump all the xml tags?
And why am I only getting one result?

And for a long time I was getting a test failure for the first case also. It
might be that I'm trying to run jdk1.6 with IntelliJ 4.5. Anyways the
problem was with the
org.apache.camel.converter.ObjectConverter.iterator(Object value) method.

I needed to change the camel code to explicitly cast the value to an
Object[] when passing an array otherwise I was getting a list of size 1 with
an Object[] in it, rather than a list of size Object[].length.


public static Iterator iterator(Object value) {
        if (value == null) {
            return Collections.EMPTY_LIST.iterator();
        } else if (value instanceof Collection) {
            Collection collection = (Collection)value;
            return collection.iterator();
        } else if (value.getClass().isArray()) {
            // TODO we should handle primitive array types?
            return Arrays.asList([Object]value).iterator();//see the
explicit cast?
        } else {
            return Collections.singletonList(value).iterator();

Weird? I made the change once but then I was able to remove the change again
so probably just an IntelliJ-ism.

See if the first test runs for you first time ;)

Here's my test case

package org.apache.camel.processor;

import org.apache.camel.*;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.builder.xml.XPathBuilder;
import org.apache.camel.component.mock.MockEndpoint;

public class NeilSplitterTest extends ContextTestSupport {
    protected Endpoint<Exchange> startEndpoint;
    protected MockEndpoint resultEndpoint;

    class CatFight {

        String name;
        String[] cats;

        public String[] getCats() {
            return cats;

        public void setCats(String[] cats) {
            this.cats = cats;

        public String getName() {
            return name;

        public void setName(String name) {
   = name;


    public void testCustomExpression() throws Exception {

        resultEndpoint.expectedBodiesReceived("Ginger", "Mr Boots");

        template.send("direct:custom", new Processor() {
            public void process(Exchange exchange) {
                Message in = exchange.getIn();
                CatFight catFight = new CatFight();
                catFight.setCats(new String[]{"Ginger", "Mr Boots"});
                in.setHeader("foo", "bar");


    public void testXPathExpression() throws Exception {

"&lt;b&gt;Mr Boots&lt;/b&gt;");

        template.send("direct:xpath", new Processor() {
            public void process(Exchange exchange) {
                Message in = exchange.getIn();
                in.setHeader("foo", "bar");


    protected void setUp() throws Exception {

        resultEndpoint = (MockEndpoint)

    protected RouteBuilder createRouteBuilder() {
        return new RouteBuilder() {
            public void configure() {
                Expression catFightCats = new Expression(){
                    public Object evaluate(Exchange exchange) {
                        CatFight catFight = (CatFight)
                        String[] cats = catFight.getCats();
                        return cats;
                XPathBuilder xPathBuilder = new XPathBuilder("a/b");

View this message in context:
Sent from the Camel - Users mailing list archive at

View raw message