Search code examples
groovycountingxmlslurper

Groovy - XmlSlurper - count node


I am a newbee to Groovy, recently I need to count the complexity of a given XML data block.

I figure out a way to determine if the data block is XML formatted or not. But I am not sure how to count all the nodes of the given XML block.

Here is my code:

    def invoke(msg)        
    { 
    try {
       contentBody = msg.get("my.message");
       new XmlSlurper().parseText(contentBody);
       Trace.debug("XML is well formed, request body is "  + contentBody);
       return true;
    }

    catch (Exception e){
        Trace.error("Invalid xml, request body is " + contentBody);
            return false;
     }      
     }

Many thanks.

Cheers, Vincent


Solution

  • Have you tried the following?

    new XmlSlurper().parseText(...).depthFirst().size()