Forum Discussion
Alex99
8 years agoContributor
Hi, I shortened the xml and added a third transaction, so that we can test the negative case also:
import javax.xml.xpath.*
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
def xml = """
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soapenv:Header/>
<soapenv:Body>
<ac:getConsumerProfileTransactionHistoryResponse xmlns:ac="http://www.moneygram.com/AgentConnect1705">
<ac:payload>
<ac:doCheckIn>true</ac:doCheckIn>
<ac:timeStamp>2018-07-18T16:37:24.679-05:00</ac:timeStamp>
<ac:flags>18</ac:flags>
<ac:mgiSessionID>443077301E15319498444757393417</ac:mgiSessionID>
<ac:GAFVersionNumber>1.1.20180612</ac:GAFVersionNumber>
<ac:numberOfRowsFound>1</ac:numberOfRowsFound>
<ac:transactions>
<ac:transaction>
<ac:currentValues>
<ac:currentValue>
<ac:infoKey>transactionStatusUpdateDate</ac:infoKey>
<ac:value>2018-07-18T14:19:30-05:00</ac:value>
</ac:currentValue>
</ac:currentValues>
</ac:transaction>
<ac:transaction>
<ac:currentValues>
<ac:currentValue>
<ac:infoKey>transactionStatusUpdateDate</ac:infoKey>
<ac:value>2018-07-18T14:19:02-05:00</ac:value>
</ac:currentValue>
</ac:currentValues>
</ac:transaction>
<ac:transaction>
<ac:currentValues>
<ac:currentValue>
<ac:infoKey>transactionStatusUpdateDate</ac:infoKey>
<ac:value>2018-07-18T22:19:02-05:00</ac:value>
</ac:currentValue>
</ac:currentValues>
</ac:transaction>
</ac:transactions>
<ac:infos/>
</ac:payload>
</ac:getConsumerProfileTransactionHistoryResponse>
</soapenv:Body>
</soapenv:Envelope>"""
def date1 = null
def date2 = null
def rootNode = new XmlSlurper().parseText(xml)
rootNode.'**'.findAll { node->
node.text() == 'transactionStatusUpdateDate'
}.each { node->
if(date1 == null) {
date1 = new Date().parse("yyyy-MM-dd'T'HH:mm:ss", node.parent().value.toString().substring(0,19))
} else {
date2 = new Date().parse("yyyy-MM-dd'T'HH:mm:ss", node.parent().value.toString().substring(0,19))
log.info("${date1.format('yyyy-MM-dd HH:mm:ss')} newer than ${date2.format('yyyy-MM-dd HH:mm:ss')} = ${checkDates(date1, date2)}")
date1 = date2
}
}
log.info "done"
def checkDates(date1, date2) {
try {
assert date1 > date2
return true
} catch (AssertionError e) {
return false
}
}
The result looks like this:
Thu Nov 01 14:12:09 CET 2018:INFO:2018-07-18 14:19:30 newer than 2018-07-18 14:19:02 = true Thu Nov 01 14:12:09 CET 2018:INFO:2018-07-18 14:19:02 newer than 2018-07-18 22:19:02 = false Thu Nov 01 14:12:09 CET 2018:INFO:done