在我的 Camel 路由期间,我查询服务器(HTTP GET),结果,我收到一个 200 OK,其 XML 正文看起来类似如下:
<?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>
知道如何获取 XML 部分中“parameter2”的值(在我的示例中为“data2”)并将该值存储在交换属性中吗?我想通过使用 xpath 表达式?或者 ...
感谢您的帮助。
检索该值的一个简单方法是使用XPath语言 http://camel.apache.org/xpath.html。它将允许您提取所需的数据并将其设置在某处(标头、正文...)。以下是如何设置参数2标头的值:
<setHeader headerName="parameter2">
<xpath resultType="java.lang.String">
/userProfiles/userProfile/userProfileAttributes/userProfileAttribute[2]/@value
</xpath>
</setHeader>
使用 Java DSL
使用 Java DSL 并设置消息正文的示例:
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(...);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)