apache camel split()vs. split(body())

时间:2015-11-03 12:35:35

标签: java apache-camel

使用拆分组件时遇到问题。我想将一个大的xml文件拆分为多个小文件。在小文件中,我想将25个元素组合在一起。我的第一个意图是使用以下内容:

from(direct:start)
.split().tokenizeXML("SomeElement", 25)
... 

但后来我收到了这个错误:

SaxParseException: Cannot find the declaration of element 'SomeElement'

然后我试了

.split(body().tokenizeXML("SomeElement", "*"))

是的,这是有效的但是split将表达式作为参数,而使用表达式body().tokenizeXML("SomeElement", "*")则没有机会使用分组功能。所以我的问题是, 1.为什么split()找不到元素? 2.是否有机会在split(Expression)中使用群组功能?

由于我们绑定Java 6,因此我们使用camel版本2.13.4。

XML文件看起来像这样(简化,想象MyRootElement中有数百个SomeElement元素)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<MyRootElement xmlns="urn:THIS:IS:A-NAMESPACE">
    <SomeElement id="12345">
        <address addressType="mainAddress" street="Test street" zipCode="12345" city="Testcity"/>
    </SomeElement>
    <SomeElement id="23456">
        <address addressType="mainAddress" street="Test street" zipCode="12345" city="Testcity"/>
    </SomeElement>
</MyRootElement>

修改

好的,在完成fiw建议的更改后,它现在可以正常工作了。

但现在我的验证失败了。虽然.split(body().tokenizeXML("SomeElement", "*"))会在SomeElement中嵌入MyRootElement,但我会收到这样的拆分消息:

<MyRootElement xmlns="urn:THIS:IS:A-NAMESPACE" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="urn:THIS:IS:A-NAMESPACE">
        <SomeElement id="12345">
            <address addressType="mainAddress" street="Test street" zipCode="12345" city="Testcity"/>
        </SomeElement>
</MyRootElement>

split().tokenizeXML("SomeElement", 2)不考虑根元素和命名空间,所以我得到这样的东西:

    <SomeElement id="12345">
                <address addressType="mainAddress" street="Test street" zipCode="12345" city="Testcity"/>
    </SomeElement>
    <SomeElement id="23456">
                <address addressType="mainAddress" street="Test street" zipCode="12345" city="Testcity"/>
    </SomeElement>

当然,如果我根据模式验证分裂的消息它会失败。因为我需要在根元素SomeElements中嵌入MyRootElement,如下所示:

<MyRootElement xmlns="urn:THIS:IS:A-NAMESPACE" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="urn:THIS:IS:A-NAMESPACE">
  <SomeElement id="12345">
            <address addressType="mainAddress" street="Test street" zipCode="12345" city="Testcity"/>
  </SomeElement>
  <SomeElement id="23456">
            <address addressType="mainAddress" street="Test street" zipCode="12345" city="Testcity"/>
  </SomeElement>
</MyRootElement>

1 个答案:

答案 0 :(得分:1)

这个分组和xml拆分测试为我传递:

public class TestSplitOnXml extends CamelTestSupport {

    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("direct:in")
                    .split().tokenizeXML("SomeElement", 2)
                        .to("mock:out")
                        .end();
            }
        };
    }

    @Test
    public void testSplitting() throws InterruptedException {
        MockEndpoint mockEndpoint = getMockEndpoint("mock:out");
        mockEndpoint.setExpectedMessageCount(2);

        Exchange exchange = createExchangeWithBody(this.getClass().getClassLoader().getResourceAsStream("text.xml"));
        template.send("direct:in", exchange);

        assertMockEndpointsSatisfied();
    }
}

将其作为test.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<MyRootElement xmlns="urn:THIS:IS:A-NAMESPACE" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="urn:THIS:IS:A-NAMESPACE">
    <SomeElement id="12345">
        <address addressType="mainAddress" street="Test street" zipCode="12345" city="Testcity"/>
    </SomeElement>
    <SomeElement id="23456">
        <address addressType="mainAddress" street="Test street" zipCode="12345" city="Testcity"/>
    </SomeElement>
    <SomeElement id="23456">
        <address addressType="mainAddress" street="Test street" zipCode="12345" city="Testcity"/>
    </SomeElement>
    <SomeElement id="23456">
        <address addressType="mainAddress" street="Test street" zipCode="12345" city="Testcity"/>
    </SomeElement>
</MyRootElement>

从中您可以看到split,tokeniseXML和分组确实有效。可能是您的xml缺少xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:THIS:IS:A-NAMESPACE"

相关问题