2015-11-03 77 views
1

我有使用拆分组件的问题。我想将一个大的XML文件分割成多个小文件。在小文件中,我想将25个元素分组在一起。使用下面的我的第一个目的是:apache camel split()vs split(body())

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,我们使用骆驼版本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> 

编辑:

OK,做由FIW建议的修改后,它的工作原理现在。

但现在我的验证失败。虽然.split(body().tokenizeXML("SomeElement", "*"))嵌入SomeElementMyRootElement,让我得到劈裂的消息是这样的:

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

你可以显示你的XML文件的例子吗? – fiw

+0

Hi fiw,我添加了一个XML片段到我原来的帖子。 – nick78

回答

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"