camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Claus Ibsen <claus.ib...@gmail.com>
Subject Re: beginner question
Date Wed, 28 Oct 2009 14:09:15 GMT
On Wed, Oct 28, 2009 at 2:58 PM, Paul Phillips <djphillyp@gmail.com> wrote:
>
> Ok great.
>
> From this I've now realised that what I really want to do is grab a bunch of
> stuff from the incoming xml and store it as properties on the message for
> future use in the route and then I don't really need the original xml
> anymore.
>
> Now I could write some code like this:
>
> DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
>    domFactory.setNamespaceAware(true); // never forget this!
>    DocumentBuilder builder = domFactory.newDocumentBuilder();
>    Document doc = builder.parse(theIncomingXML);
>
>    XPathFactory factory = XPathFactory.newInstance();
>    XPath xpath = factory.newXPath();
>    XPathExpression expr
>     = xpath.compile("//book[author='Neal Stephenson']/title/text()");
>
>    Object result = expr.evaluate(doc, XPathConstants.NODESET);
>
> and store the result objects as properties on the message.
>
> Is there an "easier" way to do this with Camel? If I am in a class that
> implements Processor, in the process method, can I access the xpath "helper"
> stuff that camel provides so that I can just (almost) say
>
> String interestingText = message.xpath("//book[author='Neal
> Stephenson']/title/text()")
>
> and it'll handle the parsing etc for me?

Yeah as you work in a Processor where you have the Exchange object handy

Something like this. You may need to tweak it a bit.

String interestingText = xpath("//book[author='Neal
Stephenson']/title/text()").evaluate(exchange, String.class);



>
>
>
>
>
>
> James.Strachan wrote:
>>
>> 2009/10/23 Paul Phillips <djphillyp@gmail.com>:
>>>
>>> Hi there
>>>
>>> bit of a beginner question here.
>>>
>>> I have a situation where I want to
>>>
>>> - get some xml from a jms queue
>>> - send an id contained in the xml to a custom component i have written
>>> (the
>>> body of the request should be a string that looks like this: "ID=blah",
>>> so I
>>> use a bean to grab the id using xpath and change the body of the
>>> exchange)
>>> - this will return a message with a body that looks like "MATCHED=TRUE"
>>> or
>>> something
>>> - if MATCHED=TRUE then I want to send some xml to a different endpoint.
>>
>> BTW you can do the above like this...
>>
>> public class MyBean {
>>
>>   @Consume(uri="activemq:SomeQueue")
>>   pubilc void onXml(@XPath("/foo/ID") String myID, Document restOfBody) {
>>     // return the transformed payload
>>     return "MATCHED=TRUE"
>>   }
>> }
>>
>> Notice Camel can do the XPath for you and inject the ID parameter to
>> your transformer method (myID) - also notice the lack of any
>> middleware APIs etc.
>>
>> for more details see
>> http://camel.apache.org/pojo-consuming.html
>>
>> --
>> James
>> -------
>> http://macstrac.blogspot.com/
>>
>> Open Source Integration
>> http://fusesource.com/
>>
>>
>
> --
> View this message in context: http://www.nabble.com/beginner-question-tp26029591p26095101.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
>



-- 
Claus Ibsen
Apache Camel Committer

Author of Camel in Action: http://www.manning.com/ibsen/
Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/
Twitter: http://twitter.com/davsclaus

Mime
View raw message