how to set jms message custom header using xpath i

2019-09-16 19:22发布

问题:

I am using camel route builder to move one activemq jms message from one queue to another by setting some custom header, by using xpath to read the node value from xml. nothing has been set. Please suggest if you know the answer.

from("activemq:com.queue1")
    .setHeader("orderNumber").xpath("/orderRequest/authNumber")
                    .to("activemq:com.queue2")
            .end();

XML would look like

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns3:orderRequest xmlns:ns2="http://www.company.com/services/entity/v1" 
                  xmlns:ns3="http://www.company.com/services/dataobject/v1">    
    <authNumber>A81585</authNumber>
</ns3:orderRequest>

回答1:

XML with namespaces requires the name spaces to be setup correctly.

You need to setup a namespace handler with something like this:

Namespaces ns = new Namespaces("ns3", "http://www.company.com/services/dataobject/v1");

....
xpath("/ns3:orderRequest/ns3:authNumber",ns)
...