我将此XML作为有效负载:
<root>
<entry>
<key>mobile_phone</key>
<value>380929993314</value>
</entry>
<entry>
<key>surname</key>
<value>Dylan</value>
</entry>
<entry>
<key>name</key>
<value>Bob</value>
</entry>
<entry>
<key>extension</key>
<value>208</value>
</entry>
<entry>
<key>mobile_extension</key>
<value>608</value>
</entry>
<entry>
<key>email</key>
<value>bob@dylan.com</value>
</entry>
</root>
和此有效载荷的Mule ESB流程:
<message-properties-transformer doc:name="Set Duty Properties" scope="session">
<add-message-property key="dutyName" value="#[xpath('/root/entry/key[text()="name"]/../value/text()').getText()]"/>
<add-message-property key="dutySurname" value="#[xpath('/root/entry/key[text()="surname"]/../value/text()').getText()]"/>
<add-message-property key="dutyMobilePhone" value="#[xpath('/root/entry/key[text()="mobile_phone"]/../value/text()').getText()]"/>
<add-message-property key="dutyEmail" value="#[xpath('/root/entry/key[text()="email"]/../value/text()').getText()]"/>
</message-properties-transformer>
这段代码工作原理很好,但有时候我的应用程序会出现这个错误:
ERROR 2013-11-17 15:42:42,712 [[duty-notifications].supportTicket.stage1.02] org.mule.exception.CatchMessagingExceptionStrategy:
********************************************************************************
Message : Execution of the expression "xpath('/root/entry/key[text()="mobile_phone"]/../value/text()').getText()" failed. (org.mule.api.expression.ExpressionRuntimeException). Message payload is of type: String
Code : MULE_ERROR--2
--------------------------------------------------------------------------------
Exception stack is:
1. [Error: unable to resolve method: java.util.ArrayList.getText() [arglength=0]]
[Near : {... xpath('/root/entry/key[text()= ....}]
^
[Line: 1, Column: 1] (org.mvel2.PropertyAccessException)
org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer:1078 (null)
2. Execution of the expression "xpath('/root/entry/key[text()="mobile_phone"]/../value/text()').getText()" failed. (org.mule.api.expression.ExpressionRuntimeException)
org.mule.el.mvel.MVELExpressionLanguage:218 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/expression/ExpressionRuntimeException.html)
3. Execution of the expression "xpath('/root/entry/key[text()="mobile_phone"]/../value/text()').getText()" failed. (org.mule.api.expression.ExpressionRuntimeException). Message payload is of type: String (org.mule.api.transformer.TransformerMessagingException)
org.mule.transformer.AbstractTransformer:139 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/transformer/TransformerMessagingException.html)
--------------------------------------------------------------------------------
Root Exception stack trace:
[Error: unable to resolve method: java.util.ArrayList.getText() [arglength=0]]
[Near : {... xpath('/root/entry/key[text()= ....}]
^
[Line: 1, Column: 1]
at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.getMethod(ReflectiveAccessorOptimizer.java:1078)
at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.getMethod(ReflectiveAccessorOptimizer.java:987)
at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.compileGetChain(ReflectiveAccessorOptimizer.java:377)
+ 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************
即。由于不明原因,dutyMobilePhone的XPath函数有时会返回ArrayList for&#34; 380929993314&#34;而不是String(通常)。
请帮帮我。如何防止XPath函数的这种随机行为?
答案 0 :(得分:2)
在您的Xpath表达式中,选择包含元素并从MEL获取其文本内容:
value="#[xpath('/root/entry/key[text()="mobile_phone"]/../value').text]"
答案 1 :(得分:0)
通过在XML中添加<?xml version="1.0" encoding="utf-8"?>
标头解决。没有这个Mule XPath函数表现得很奇怪。