Ask a Question

How to retrieve Node Tree when child value matches text

SOLVED
chriscropley
Occasional Contributor

How to retrieve Node Tree when child value matches text

Dear Smartbear community,

 

it has been a while since I have been at the coal face however I have searched far and wide for a response yet I can't quite find a solution to my problem.  I hope someone can help.  I am using groovy to parse an API call response in xml.  An example is below:

 

<GetJobs>
<jobs>
<jobEntry>
<name>JOB_NAME</name>
<status>RUNNING</status>
<runDate>2021-08-31T19:58:27+10:00</runDate>
<runDuration>0 days 00:47:14</runDuration>
<chainName>JOB_NAME_CHAIN</chainName>
<runningStep>STEP_FOUR</runningStep>
<steps>
<stepEntry>
<stepName>STEP_ONE</stepName>
<stepState>SUCCEEDED</stepState>
</stepEntry>
<stepEntry>
<stepName>STEP_TWO</stepName>
<stepState>SUCCEEDED</stepState>
</stepEntry>
<stepEntry>
<stepName>STEP_THREE</stepName>
<stepState>FAILED</stepState>
</stepEntry>
<stepEntry>
<stepName>STEP_FOUR</stepName>
<stepState>RUNNING</stepState>
</stepEntry>
<stepEntry>
<stepName>STEP_FIVE</stepName>
<stepState>NOT_STARTED</stepState>
</stepEntry>
</steps>
</jobEntry>
</jobs>
<jobStatus>RUN</jobStatus>
<startDate>2021-08-31</startDate>
<jobName/>
<returnType>NEW</returnType>
</GetJobs>

 

I essentially want to pull out the <name> and <stepName> for the 'FAILED' steps (i.e. STEP_THREE).  Or ALL <stepNames> that meet the search string, like 'RUNNING' or 'FAILED'

 

So it might print line "Job " + node.name.text() + " Step " + node.stepname.text() + " Failed."

"Job JOB_NAME Step STEP_THREE Failed."

 

As I say it has been a while since I have needed to solve these problems and I am a bit rusty.  I am a big flumoxed and any help would be much appreciated.

1 REPLY 1
chriscropley
Occasional Contributor

Thanks to some valuable contributions from other sites, I managed to find a solution.  It was fairly easy in the end stiching some ideas from those posts.  I have put it here in case anyone would like to make use of it.

 

import groovy.util.*
import java.text.SimpleDateFormat

def sdfDateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss")

//Parse XML content in the response
def completedResponse = context.expand( '${ListCompletedStreams#Response}' )
def parsedcompletedResponse = new XmlSlurper().parseText(completedResponse)

//Loop over each Job Entry
parsedcompletedResponse.jobs.jobEntry.each { jobEntry ->

//Loop over every steo entry and pull out only those steps that fail. Find the parent Job Name and other parent properties.
jobEntry.steps.stepEntry.each { stepEntry ->
if (stepEntry.stepState.text() == "FAILED" ) {

def strJobEntryRunDate = Date.parse("yyyy-MM-dd'T'HH:mm:ss", "${jobEntry.runDate.text()}")
log.info "\tJob Stream : ${jobEntry.name.text()} starting at " + sdfDateFormat.format(strJobEntryRunDate) + " has a failed Job Step."
log.info "\tJob Step: ${stepEntry.stepName.text()} failed after ${jobEntry.runDuration.text()} minutes."
}
}
}


return null

cancel
Showing results for 
Search instead for 
Did you mean: