Camel Apache:从接收到的 XML 中提取一些值的 xpath [英] Camel Apache: xpath to extract some value out of received XML

查看:43
本文介绍了Camel Apache:从接收到的 XML 中提取一些值的 xpath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Camel 路由期间,我查询了一个服务器(一个 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>

知道如何在 XML 部分(在我的示例中为data2")中获取parameter2"的值并将该值存储在交换属性中吗?我想通过使用 xpath 表达式?或者 ...感谢您的帮助.

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 语言.它将允许您提取所需的数据并将其设置在某处(header、body 等).以下是如何使用值设置 parameter2 标头:

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 header 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(...);

这篇关于Camel Apache:从接收到的 XML 中提取一些值的 xpath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆