xpath - Camel Apache : xpath to extract some value out of received XML

标签 xpath apache-camel

在我的 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/

相关文章:

apache-camel - InOut是否将out消息反馈给消费者或生产者?

xml - 使用 XPath 选择 XML 节点时如何忽略命名空间

xpath - 如何使用xpath查询获取具有相同名称模式的所有子节点的属性?

java - 如何使用java从XML读取父节点的子节点

java - Camel Bindy 固定长度格式 : How to use inherited classes?

java - 使用 MockEndpoints 测试 Camel

java - Camel会自动生成SOAP消息吗?

ruby - 检查元素是否有两个类

java - 如何使用 selenium Webdriver 单击单选按钮?

java - Apache camel 和 SQS 扩展客户端库