骆驼阿帕奇:XPath来提取一些价值上收到的XML [英] Camel Apache: xpath to extract some value out of received XML
问题描述
在我的骆驼的路线,我查询服务器(HTTP GET),并作为一个结果,我收到了200 OK与XML的身体看起来像这样类似的:
during my Camel routes, I query a server (a HTTP GET) and as a result, I receive a 200 OK with a XML body looking similar 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>
任何想法,我将如何能够得到参数2中的XML部分的价值(在我的例子数据2)和值存储在一个交流的财产?我想通过使用XPath前pression?要么 ...
感谢您的帮助。
Any idea how I would be able to get the value of "parameter2" in the XML part (in my example 'data2') and store that value in an exchange property ? I guess by using an xpath expression ? Or ... Thanks for your help.
推荐答案
这是简单的方法来检索值将使用的 XPath语言。它将允许你提取所需的数据,并设置它的地方(标题,正文,...)。下面是如何设置的参数2 与价值headet:
An easy way to retrieve the value would be to use the XPath Language. It will allow you to extract the data you want and set it somewhere (header, body , ...). Here is how to set a parameter2 headet with the value:
<setHeader headerName="parameter2">
<xpath resultType="java.lang.String">
/userProfiles/userProfile/userProfileAttributes/userProfileAttribute[2]/@value
</xpath>
</setHeader>
使用Java DSL
使用Java DSL和设置消息体的一个例子:
An example using the 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(...);
这篇关于骆驼阿帕奇:XPath来提取一些价值上收到的XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!