Ask a Question

how do build a string of same nodes from JDBC response

zsousa
Contributor

how do build a string of same nodes from JDBC response

I want to create a string of all the nodes that are the same. Using my example below, I want a string of all nodes that have more than one same name. For example, using the JDBC response below, I would want to create two strings. One for TRIP.ID and another string for all TRIP.SEQUENCE_NUM

 

Can anyone guide me through this ?



<Results>
<ResultSet fetchSize="0">
<Row rowNumber="1">
<TRIP.ID>80199662</TRIP.ID>
<TRIP.SEQUENCE_NUM>6</TRIP.SEQUENCE_NUM>
</Row>
<Row rowNumber="2">
<TRIP.ID>80199661</TRIP.ID>
<TRIP.SEQUENCE_NUM>5</TRIP.SEQUENCE_NUM>
</Row>
<Row rowNumber="3">
<TRIP.ID>80199660</TRIP.ID>
<TRIP.SEQUENCE_NUM>4</TRIP.SEQUENCE_NUM>
</Row>
<Row rowNumber="4">
<TRIP.ID>80199659</TRIP.ID>
<TRIP.SEQUENCE_NUM>3</TRIP.SEQUENCE_NUM>
</Row>
<Row rowNumber="5">
<TRIP.ID>80199658</TRIP.ID>
<TRIP.SEQUENCE_NUM>2</TRIP.SEQUENCE_NUM>
</Row>
<Row rowNumber="6">
<TRIP.ID>80199657</TRIP.ID>
<TRIP.SEQUENCE_NUM>1</TRIP.SEQUENCE_NUM>
</Row>
</ResultSet>
</Results>

1 REPLY 1
Radford
Super Contributor

Re: how do build a string of same nodes from JDBC response

I'm not completely sure of what you are trying to do, but hopefully this will give you a start.

 

Using a Groovy test step there are a few ways to manipulate XML, I personally prefer the XmlSlurper. Here is an example of looping through the example JDBC result in your post:

 

import groovy.util.XmlSlurper

// Substitute your JDBC test name below
def responseAsXml = context.expand( '${JDBC Request#ResponseAsXml}' )

def Results = new XmlSlurper().parseText(responseAsXml)

Results.ResultSet.Row.each{ Row -> 
	// Normally wouldn't need the quotes around the XML element name,
	// but we need them here because the element names contain a period.
	log.info('Row ' + Row.@rowNumber + ' Id = ' + Row.'TRIP.ID'.text())
	log.info('Row ' + Row.@rowNumber + ' Sequence = ' + Row.'TRIP.SEQUENCE_NUM'.text())
}

 

An alternative method, would be to use an XmlHolder, an example of using this to iterate through nodes can be found here.

cancel
Showing results for 
Search instead for 
Did you mean: