3

I have a simple flow that reads from an activemq1 and parses an id from the message and sends the id across to another activemq2.

Here is an example of message that gets written to activemq1:

<Customer>
  <Order>
     <id>123</id>
  </Order>
<Customer>

I need to parse out the id from the above message body and send the below message to activemq2:

"Order with id{123} has been queued" 

This is the flow that I cameup with, but it writes the complete request xml to the queue, but not the message that I am looking for:

<from uri="jms:queue:Q.activemq1"/>
   <setBody>
        <xpath>"/Customer/Order/id/@value/text()"</xpath>
    </setBody>
 <to uri="jms:queue:Q.activemq2"/>

Anything wrong in the above or any corrections

1 Answer 1

4

Use

<route>
    <from uri="jms:queue:Q.activemq1" />
    <setBody>
        <xpath resultType="java.lang.String">/Customer/Order/id/text()</xpath>
    </setBody>
    <setBody>
        <simple>Order with ${body} has been queued</simple>
    </setBody>
    <log message="${body}"/>
</route>

This prints

Order with 123 has been queued
Sign up to request clarification or add additional context in comments.

8 Comments

Hello, The intention is to send the message(Order with 123 has been queued) accross to activemq2. So, how to concatenate {123} with the rest of the message.
A good solution. However, I would suggested to use string() instead of text(), which is better in almost all cases (e.g. if you use `text() you might get back comments in your XML as result, which is almost never intended)
seems like xpath is not being applied at all. I get the complete xml being transfered <Customer> <Order> <id>123</id> </Order> <Customer>
This is the error..looks like it is treating it as a dob object instead of a string/text caught: org.apache.camel.builder.xml.InvalidXPathExpression: Invalid xpath: /Customer/Order/id/text(). Reason: javax.xml.xpath.XPathExpressionException: org.xml.sax.SAXParseException: XML document structures must start and end within the same entity.
I erred...This is the exact error that iam seeing...Using converter: InstanceMethodTypeConverter: public java.lang.String org.apache.camel.converter.jaxp.DomConverter.toString(org.w3c.dom.NodeList,org.apache.camel.Exchange) throws javax.xml.transform.TransformerException to convert [class org.apache.xml.dtm.ref.DTMNodeList=>class java.lang.String]
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.