Forum Discussion

sanjeevkumar7's avatar
sanjeevkumar7
Occasional Contributor
14 years ago

Count number of child nodes

How to find the number of child nodes in a xml node.

For example : I can find the number of child nodes using xmlslurper() the below way.

def path = new XmlSlurper().parse(new File('C://SoapUI//ResponseXML/Response.xml'))
NumberOfPositions = path.Body.GetPositionsAggregateResponse.GetPositionsAggregateResult.AccountPositions.Securities.Positions.children().size()


I'm able to get the value using "holder" (groovyutils.getxmlholder) with the below code.

positionid1 = holder.getNodeValue("//ns1:GetPositionsAggregateResponse[1]/ns1:GetPositionsAggregateResult[1]/a:AccountPositions[1]/a:Securities[1]/a:Positions[1]/a:PositionSummary[1]/a:Legs[1]/a:PositionAggregate[1]/a:PositionID[1]")
log.info ("$positionid1")


Could you please let me know how to get the number of child nodes using the holder?

Thanks!
Sanjeev

1 Reply

  • sanjeevkumar7's avatar
    sanjeevkumar7
    Occasional Contributor
    Ah never mind, figured that out. Just had to do some trial and error

    treesize = holder.getNodeValues("//ns1:GetPositionsAggregateResponse[1]/ns1:GetPositionsAggregateResult[1]/a:AccountPositions[1]/a:Securities[1]/count(*)")