Ask a Question

Groovy get node value

saiyan_prince
New Member

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!!

3 REPLIES 3
Lucian
Community Hero

Re: Groovy get node value

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.



If my answer helped please click on the 'Accept as Solution' button.
ashu248
Contributor

Re: Groovy get node value

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
Moderator

Re: Groovy get node value

 

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,

 


Olga Terentieva
SmartBear Assistant Community Manager

cancel
Showing results for 
Search instead for 
Did you mean: