首页 文章

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

提问于
浏览
1

我在使用拆分组件时遇到问题 . 我想将一个大的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>

EDIT:

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

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

<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>

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

<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 回答

  • 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"

相关问题