我使用带有xpath-expression
<int:chain input-channel="routerInputChannel">
<int-xml:xpath-router default-output-channel="routerOutputChannel">
<int-xml:xpath-expression expression="//actor[1]/text()"/>
<int-xml:mapping value="Christian Bale" channel="routerBaleChannel"/>
</int-xml:xpath-router>
</int:chain>
并期望此XML字符串作为SI消息有效负载
<root xmlns:foo="http://www.foo.org/" xmlns:bar="http://www.bar.org">
<actors>
<actor id="1">Christian Bale</actor>
<actor id="2">Liam Neeson</actor>
<actor id="3">Michael Caine</actor>
</actors>
<foo:singers>
<foo:singer id="4">Tom Waits</foo:singer>
<foo:singer id="5">B.B. King</foo:singer>
<foo:singer id="6">Ray Charles</foo:singer>
</foo:singers>
</root>
将发送给routerBaleChannel
但由于某种原因它无法正常工作。
我在XPath Tester / Evaluator尝试了邮件和XPath表达式,正如预期的那样,我已经获得了Christian Bale
字符串。
我调试了org.springframework.integration.xml.router.XPathRouter
方法getChannelKeys
并在Jaxp13XPathExpressionFactory
方法evaluate
中看到了一些问题:执行后出于未知原因
NodeList nodes = (NodeList) evaluate(node, XPathConstants.NODESET);
nodes.getLength()
返回空列表。
那么我的XPath路由器或XPath表达式配置有什么问题?
答案 0 :(得分:0)
适合我:
<int:chain input-channel="inputChannel">
<int-xml:xpath-router>
<int-xml:xpath-expression expression="//actor[1]/text()"/>
<int-xml:mapping value="Christian Bale" channel="routerBaleChannel"/>
</int-xml:xpath-router>
</int:chain>
<int:channel id="routerBaleChannel">
<int:queue/>
</int:channel>
...
@Test
public void testSO45173221() {
ClassPathXmlApplicationContext ac =
new ClassPathXmlApplicationContext("XPathRouterTests-context.xml", this.getClass());
MessageChannel inputChannel = ac.getBean("inputChannel", MessageChannel.class);
PollableChannel channelBale = ac.getBean("routerBaleChannel", PollableChannel.class);
GenericMessage<String> message =
new GenericMessage<>("<root xmlns:foo=\"http://www.foo.org/\" xmlns:bar=\"http://www.bar.org\">\n" +
" <actors>\n" +
" <actor id=\"1\">Christian Bale</actor>\n" +
" <actor id=\"2\">Liam Neeson</actor>\n" +
" <actor id=\"3\">Michael Caine</actor>\n" +
" </actors>\n" +
" <foo:singers>\n" +
" <foo:singer id=\"4\">Tom Waits</foo:singer>\n" +
" <foo:singer id=\"5\">B.B. King</foo:singer>\n" +
" <foo:singer id=\"6\">Ray Charles</foo:singer>\n" +
" </foo:singers>\n" +
"</root>");
inputChannel.send(message);
Message<?> result = channelBale.receive(0);
assertNotNull(result);
assertEquals(message.getPayload(), result.getPayload());
ac.close();
}
不确定你的情况发生了什么......
虽然我使用的是Java 8 FYI。