【问题标题】:Split xml file in camel using the .split().tokenizeXML()?使用 .split().tokenizeXML() 在骆驼中拆分 xml 文件?
【发布时间】:2015-04-25 10:33:21
【问题描述】:

如何使用 .split().tokenizeXML() 在骆驼中拆分 xml 文件?我附上了代码sn-p。我不知道我在哪里做错了。 这是我的意见。

<Record>
  <DataFile xmlns="Created">
  </DataFile>
  <DataFile xmlns="Updated">
  </DataFile>
  <DataFile xmlns="Deleted">
  </DataFile>
</Record>

这是我的骆驼路线

// Main Route
from(...)
.routeId("processor route")
.process(...)
.to("direct:created",
"direct:updated",
"direct:deleted").end();

// Created
from("direct:created")
.routeId("created route")
.split().tokenizeXML("xmlns:Created", "Record")
.to(...).end();

// Updated
from("direct:updated")
.routeId("updated route")
.split().tokenizeXML("xmlns:Updated", "Record")
.to(...).end();

// Deleted
from("direct:deleted")
.routeId("deleted route")
.split().tokenizeXML("xmlns:Deleted", "Record")
.to(...).end();

我的预期输出是... direct:created 应该只拆分和使用这个。

<DataFile xmlns="Created">
</DataFile>

direct:updated 应该只拆分和使用这个。

<DataFile xmlns="Updated">
</DataFile>

and direct:deleted 应该只拆分和使用这个。

<DataFile xmlns="Deleted">
</DataFile> 

【问题讨论】:

    标签: java xml split apache-camel dsl


    【解决方案1】:

    您不能使用 tokenizeXml 按命名空间进行拆分。您需要自己拆分文件,或者编写一些可以按命名空间拆分的代码。

    【讨论】:

    • 谢谢...camel dsl中还有其他选项可以拆分这种XML吗?
    【解决方案2】:

    我不知道如何在 XPath 中获取“xmlns”属性的值,因为“xmlns”是一个 NameSpace 属性。如果您可以将该属性的名称更改为例如“属性”你可以这样使用:

    首先将 xml 拆分为元素列表“DataFile”,然后使用“attribute”(“attribute”)的值使用基于内容的路由,因为我不知道如何在 XPath 中获取“xmlns”属性的值 - 你可以自己找到这个并尝试)

    from("direct:route").split().tokenizeXML("DataFile").streaming().choice()
        .when().xpath("//DataFile[@attribute=&#39;Created&#39;]").to("direct:created")
        .when().xpath("//DataFile[@attribute=&#39;Updated&#39;]").to("direct:updated")
        .otherwise().to("direct:deleted")
    

    【讨论】: