Java 1.8 Groovy版本2.4.7
我将xml传递给名为rollbackxmlResp的变量,我正在尝试解析并获取它的值。
Groovy代码 我收到此异常 请帮我解决这个问题。rollbackXmlResp
的<?xml version=\"1.0\" encoding=\"EUC-JP\"?>
<Root>
<data>
<easy_id>12214356</easy_id>
<unique_id>53706741</unique_id>
<rollback_all_point>100</rollback_all_point>
<rollback_term_point>10</rollback_term_point>
<rollback_lapse_point>20</rollback_lapse_point>
<res_time>2014-05-01 10:29:52</res_time>
<result_code>0</result_code>
</data>
<confirmation_key>ea7784d7d1d80cf94a4066ac48fa3088</confirmation_key>
</Root>
public static Map<String, ?> processRollbackResponse(String rollbackXmlResp, String requestTime){
Map rootMap = new LinkedHashMap();
def responseXml = new XmlParser().parseText(rollbackXmlResp);
responseXml.children().each { --> line no 172
def errorCodesList = new ArrayList<String>()
it.depthFirst().each { --> line no 174
switch(it.name()){ ----> failing here . line no175
}
}
}
return rootMap
}
groovy.lang.MissingMethodException: No signature of method: java.lang.String.name() is applicable for argument types: () values: []
Possible solutions: take(int), any(), any(groovy.lang.Closure), wait(), size(), dump()
at co.xx.app.point.util.Random.processRollbackResponse_closure2$_closure6(Random.groovy:175)
at groovy.lang.Closure.call(Closure.java:414)
at groovy.lang.Closure.call(Closure.java:430)
at co.xx.app.point.util.Random.processRollbackResponse_closure2(Random.groovy:174)
at groovy.lang.Closure.call(Closure.java:414)
at groovy.lang.Closure.call(Closure.java:430)
at co.xx.app.point.util.xx.processRollbackResponse(xx.groovy:172)
at co.xx.app.point.util.xxSpock.processRollbackSuccessResponse(xxSpock.groovy:246)
答案 0 :(得分:0)
考虑到以下修改后的代码版本:
def str = """\
<?xml version=\"1.0\" encoding=\"EUC-JP\"?>
<Root>
<data>
<easy_id>12214356</easy_id>
<unique_id>53706741</unique_id>
<rollback_all_point>100</rollback_all_point>
<rollback_term_point>10</rollback_term_point>
<rollback_lapse_point>20</rollback_lapse_point>
<res_time>2014-05-01 10:29:52</res_time>
<result_code>0</result_code>
</data>
<confirmation_key>ea7784d7d1d80cf94a4066ac48fa3088</confirmation_key>
</Root>
"""
def processRollbackResponse(String rollbackXmlResp, String requestTime) {
def rootMap = [:]
def responseXml = new XmlParser().parseText(rollbackXmlResp)
responseXml.children().each { child ->
println "child: ${child.name()}"
def errorCodesList = []
child.depthFirst().each { node ->
println " node class: ${node.getClass().name} - node: $node"
}
}
return rootMap
}
processRollbackResponse(str, "sometime")
我们得到以下输出:
child: data
node class: groovy.util.Node - node: data[attributes={}; value=[easy_id[attributes={}; value=[12214356]], unique_id[attributes={}; value=[53706741]], rollback_all_point[attributes={}; value=[100]], rollback_term_point[attributes={}; value=[10]], rollback_lapse_point[attributes={}; value=[20]], res_time[attributes={}; value=[2014-05-01 10:29:52]], result_code[attributes={}; value=[0]]]]
node class: groovy.util.Node - node: easy_id[attributes={}; value=[12214356]]
node class: groovy.util.Node - node: unique_id[attributes={}; value=[53706741]]
node class: groovy.util.Node - node: rollback_all_point[attributes={}; value=[100]]
node class: groovy.util.Node - node: rollback_term_point[attributes={}; value=[10]]
node class: groovy.util.Node - node: rollback_lapse_point[attributes={}; value=[20]]
node class: groovy.util.Node - node: res_time[attributes={}; value=[2014-05-01 10:29:52]]
node class: groovy.util.Node - node: result_code[attributes={}; value=[0]]
child: confirmation_key
node class: groovy.util.Node - node: confirmation_key[attributes={}; value=[ea7784d7d1d80cf94a4066ac48fa3088]]
node class: java.lang.String - node: ea7784d7d1d80cf94a4066ac48fa3088
换句话说,depthFirst将返回当前节点,然后返回所有后代。第二次迭代将为您提供Node&#34; configuration_key&#34;然后是字符串&#34; ea7784d7d1d80cf94a4066ac48fa3088&#34;。
因此发生异常是因为对于每个depthFirst的最后一次迭代,&#39;节点的值为&#39;变量(或代码中的第二个&#39;是字符串&#34; ea7784d7d1d80cf94a4066ac48fa3088&#34;而不是groovy.util.Node。
我不得不承认,从深度开始,这是不直观的行为,至少可以说。它首次使用data,easy_id,unique_id等,并且不会遍历这些节点的节点文本。第二次为configuration_key提供当前节点,然后是节点文本...即。与第一次做的完全相反。
似乎others have noted this inconsistency as well。
如果在代码中用XmlSlurper替换XmlParser,则行为将更符合您对depthFirst的每次迭代(包括最后一次迭代)返回的NodeChild所期望的行为。