Inconsistent return from XpathBuilder.evaluate()

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Inconsistent return from XpathBuilder.evaluate()

Djeeaie .
Hello,

while trying to parse xml outside a camel route using xpath (as
described here :
http://camel.apache.org/xpath.html#XPath-UsingXPathBuilderwithoutanExchange),
I met an issue while trying to set the type of the return.
It seems that if you use the setResultType(Class<T> resultType) before
the evaluate, it will use the type correclty but will only return the
first element. But if you use evaluate(CamelContext context, Object
body, Class<T> type), it will return all elements at once.

I included a short snippet illustrating the issue I encountered :

    CamelContext cc = new DefaultCamelContext();
    String xml = "<xml><a>1</a><a>2</a></xml>";
    XPathBuilder xpb = new XPathBuilder("/xml/a/text()");
    System.out.println(xpb.evaluate(cc, xml, String.class));
    xpb.setResultType(String.class);
    System.out.println(xpb.evaluate(cc, xml));

String.class can be replaced by List.class and will return a List with
2 elements in the first case, and a null in the second.

I encountered this behaviour in Camel 2.15.2 and wanted to know if
it's a normal behaviour and I missed a way to set the result type
outside of the evaluate method and still get all nodes in one call.
Reply | Threaded
Open this post in threaded view
|

Re: Inconsistent return from XpathBuilder.evaluate()

Claus Ibsen-2
Hi

Thanks for reporting. It seems like a little bug
https://issues.apache.org/jira/browse/CAMEL-9079

On Tue, Aug 11, 2015 at 2:49 PM, Djeeaie . <[hidden email]> wrote:

> Hello,
>
> while trying to parse xml outside a camel route using xpath (as
> described here :
> http://camel.apache.org/xpath.html#XPath-UsingXPathBuilderwithoutanExchange),
> I met an issue while trying to set the type of the return.
> It seems that if you use the setResultType(Class<T> resultType) before
> the evaluate, it will use the type correclty but will only return the
> first element. But if you use evaluate(CamelContext context, Object
> body, Class<T> type), it will return all elements at once.
>
> I included a short snippet illustrating the issue I encountered :
>
>     CamelContext cc = new DefaultCamelContext();
>     String xml = "<xml><a>1</a><a>2</a></xml>";
>     XPathBuilder xpb = new XPathBuilder("/xml/a/text()");
>     System.out.println(xpb.evaluate(cc, xml, String.class));
>     xpb.setResultType(String.class);
>     System.out.println(xpb.evaluate(cc, xml));
>
> String.class can be replaced by List.class and will return a List with
> 2 elements in the first case, and a null in the second.
>
> I encountered this behaviour in Camel 2.15.2 and wanted to know if
> it's a normal behaviour and I missed a way to set the result type
> outside of the evaluate method and still get all nodes in one call.



--
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2nd edition: http://www.manning.com/ibsen2