groovy.lang.MissingMethodException:方法java.lang.String.name()的签名不适用于参数类型:()值

groovy.lang.MissingMethodException:方法java.lang.String.name()的签名不适用于参数类型:()值,groovy,xml-parsing,java-8,closures,Groovy,Xml Parsing,Java 8,Closures,Java 1.8 Groovy版本2.4.7 我将xml传递给一个名为rollbackxmlResp的变量,我试图解析并获取它的值 回滚xmresp的Xmlcontents: <?xml version=\"1.0\" encoding=\"EUC-JP\"?> <Root> <data> <easy_id>12214356</easy_id> <unique

Java 1.8 Groovy版本2.4.7

我将xml传递给一个名为rollbackxmlResp的变量,我试图解析并获取它的值

回滚xmresp的Xmlcontents

<?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 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")
换句话说,depthFirst将返回当前节点,然后返回所有子节点。第二次迭代将为您提供节点“configuration_key”,然后是字符串“ea7784d7d1d80cf94a4066ac48fa3088”

因此出现异常是因为对于depthFirst的最后一次迭代,node变量(或代码中的第二个it)的值是字符串“ea7784d7d1d80cf94a4066ac48fa3088”,而不是groovy.util.node

我必须承认,至少可以说,这是深度的不直观行为。它第一次访问数据、easy_id、unique_id等,而不遍历这些节点的节点文本。第二次使用configuration_键时,它会给出当前节点,然后是节点文本…即。与第一次完全相反

似乎


如果在代码中用XmlSlurper替换XmlParser,那么对于depthFirst的每次迭代(包括最后一次迭代)返回的NodeChild,行为将更符合预期

你需要什么价值?
    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)
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