在我的 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 部分(在我的示例中为“data2”)中获取“parameter2”的值并将该值存储在交换属性中吗?我想通过使用 xpath 表达式?或者 ...
谢谢你的帮助。
最佳答案
检索值的一种简单方法是使用 XPath Language .它将允许您提取所需的数据并将其设置在某处(header、body 等)。以下是如何设置 参数 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(...);
关于xpath - Camel Apache : xpath to extract some value out of received XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9636654/