0

Java 1.8 Groovy Version 2.4.7

I am passing an xml to a variable called rollbackxmlResp, i am trying to parse and get the value of it.

Xmlcontents of 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>

Groovy code

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

    }

I am getting this exception

    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)

Please help me to solve this.

the_code
  • 85
  • 2
  • 10

1 Answers1

0

Given the following somewhat modified version of your code:

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")

we get the following output:

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

in other words, depthFirst will return the current node and then all descendants. The second iteration will give you the Node "configuration_key" and then the String "ea7784d7d1d80cf94a4066ac48fa3088".

So the exception occurs because for the last iteration of the depthFirst each, the value of the 'node' variable (or the second 'it' in your code) is the string "ea7784d7d1d80cf94a4066ac48fa3088" and not a groovy.util.Node.

I have to admit that this is unintuitive behavior from depthFirst to say the least. It goes data, easy_id, unique_id, etc the first time around and does not iterate through the node text for these nodes. The second time around for configuration_key it gives you the current node and then the node text...i.e. the exact opposite of what it did the first time around.

Seems others have noted this inconsistency as well.

If you replace XmlParser with XmlSlurper in your code, the behavior will be more in line with what you would expect with a NodeChild returned for each iteration of the depthFirst, including the last one.

Community
  • 1
  • 1
Matias Bjarland
  • 4,124
  • 1
  • 13
  • 21