我在使用拆分组件时遇到了问题。我想将一个大型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。
imagine看起来如下(简化了,假设MyRootElement中有数百个MyRootElement元素)
<?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>发布于 2015-11-04 20:35:52
分组和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>从这一点可以看出,拆分、tokeniseXML和分组确实有效。可能您的xml只是缺少了xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:THIS:IS:A-NAMESPACE"。
https://stackoverflow.com/questions/33499312
复制相似问题