Camel Apache: xpath to extract some value from the resulting XML

during my Camel routes, I request a server (HTTP GET), and as a result I get 200 OK with an XML body that looks like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <userProfiles xmlns="http://www.mycompany.com/AEContext/xmldata"> <userProfile name="guest"> <userProfileAttributes> <userProfileAttribute name="parameter1" value="data1" nameVisibility="ALL"/> <userProfileAttribute name="parameter2" value="data2" nameVisibility="ALL"/> <userProfileAttribute name="parameter3" value="data3" nameVisibility="ALL"/> </userProfileAttributes> </userProfile> </userProfiles> 

Any idea how I can get the value of parameter "parameter2" in the XML part (in my example "data2") and store this value in the exchange property? Think using the xpath expression? Or ... Thank you for your help.

+4
source share
1 answer

An easy way to get the value is to use XPath Language . This will allow you to extract the necessary data and install it somewhere (header, body, ...). Here's how to set parameter2 header with value:

 <setHeader headerName="parameter2"> <xpath resultType="java.lang.String"> /userProfiles/userProfile/userProfileAttributes/userProfileAttribute[2]/@value </xpath> </setHeader> 

Using Java DSL

An example of using Java DSL and setting the message body:

 final Namespaces ns = new Namespaces("c", "http://www.mycompany.com/AEContext/xmldata"); // existing code from(...) .setBody( ns.xpath( "/c:userProfiles/userProfile/userProfileAttributes/userProfileAttribute[2]/@value", String.class) ) .to(...); 
+9
source

Source: https://habr.com/ru/post/1400600/


All Articles