Forum Discussion

saiyan_prince's avatar
saiyan_prince
New Member
7 years ago

Groovy get node value

Hi,

 

I'm writing a groovy script to read a JDBC request. I'm having a little trouble in getting the node values.

<Row rowNumber="1">
         <JOB_ID>1</JOB_ID>
         <JOB_TITLE>Public Accountant</JOB_TITLE>
         <MIN_SALARY>4200.00</MIN_SALARY>
         <MAX_SALARY>9000.00</MAX_SALARY>
</Row>

I want to read the elements inside the Row. getNodeValue() returns null.

 

def groovyutils = new com.eviware.soapui.support.GroovyUtils(context)

def testCase = testRunner.testCase

def testStep = testCase.getTestStepByName("JDBC Request")

def runner = testStep.run(testRunner, context)

def response = runner.getResponseContentAsXml();

def xml = groovyutils.getXmlHolder(response);

for( node in xml.getDomNodes("/Results/ResultSet/Row")) {
	def nodeList = node.getChildNodes()
   	def jobid = node.getChildNodes().item(1).getNodeValue()
   	log.info(jobid)   
def jobtitle = node.getChildNodes().item(2).getNodeValue()
log.info(jobtitle) }

Log:

Wed Aug 01 00:09:36 IST 2018: INFO: null
Wed Aug 01 00:09:36 IST 2018: INFO:
Wed Aug 01 00:09:36 IST 2018: INFO: null
Wed Aug 01 00:09:36 IST 2018: INFO:
Wed Aug 01 00:09:36 IST 2018: INFO: null
Wed Aug 01 00:09:36 IST 2018: INFO:

 I want to read each element in the Row. 

Help will be appreciated!!

  • Lucian's avatar
    Lucian
    Community Hero

    Hey,

     

    I wrote an article about this here. You just need to change 

    def xml = new XmlSlurper().parse("...")

    to 

    def xml = new XmlSlurper().parseText("...")

    and you should be fine.

     

    Let me know if you need more help.

  • def str='<Row rowNumber="1"><JOB_ID>1</JOB_ID> <JOB_TITLE>Public Accountant</JOB_TITLE><MIN_SALARY>4200.00</MIN_SALARY><MAX_SALARY>9000.00</MAX_SALARY></Row>'

     

    def xml=new XmlSlurper().parseText(str)
    xml.depthFirst().each{
    log.info "tagName : "+it.name()+" = "+it.text()
    }

     

    above code will return node name with node value.

    • Olga_T's avatar
      Olga_T
      SmartBear Alumni (Retired)

       

      Thank you everyone who has looked into the issue.

       

      saiyan_prince, could you please mark this topic as Solved? Or, if you need any further assistance regarding this, please drop a line here.

       

      Thanks,