How to get the tag values of the parent tag when parent tag attribute value is unique?

Refresh

March 2019

Views

49 time

1

I have a SOAP XML data like this.

<Results>
    <ResultSet fetchSize="10">
             <Row rowNumber="1">
                <REL_ID>22439129</REL_ID>
                <EFF_TMSTP>2015-09-14 07:08:31.246</EFF_TMSTP>
                <RETIRED_TRAN_ID>63859659</RETIRED_TRAN_ID>
                <OBJ_ID>1371718</OBJ_ID>
                <REL_OBJ_ID>1350658</REL_OBJ_ID>
                <CUST_VIEW_CD>CORE</CUST_VIEW_CD>
                <CUST_MODEL_CD>CESE</CUST_MODEL_CD>
                <TYPE_CD>DRVD</TYPE_CD>
                <UNLINK_FLG>N</UNLINK_FLG>
                <EXPR_TMSTP>9999-12-31 23:59:59.999</EXPR_TMSTP>
                <TRAN_TMSTP>2015-09-14 07:08:31.246</TRAN_TMSTP>
                <RETIRED_TRAN_TMSTP>9999-12-31 23:59:59.999</RETIRED_TRAN_TMSTP>
                <TRAN_ID>63859659</TRAN_ID>
            </Row>
            <Row rowNumber="2">
                <REL_ID>22482704</REL_ID>
                <EFF_TMSTP>2015-09-24 06:27:43.358</EFF_TMSTP>
                <RETIRED_TRAN_ID>64285012</RETIRED_TRAN_ID>
                <OBJ_ID>11983064</OBJ_ID>
                <REL_OBJ_ID>1350658</REL_OBJ_ID>
                <CUST_VIEW_CD>CORE</CUST_VIEW_CD>
                <CUST_MODEL_CD>CESE</CUST_MODEL_CD>
                <TYPE_CD>DRVD</TYPE_CD>
                <UNLINK_FLG>N</UNLINK_FLG>
                <EXPR_TMSTP>9999-12-31 23:59:59.999</EXPR_TMSTP>
                <TRAN_TMSTP>2015-09-24 06:27:43.358</TRAN_TMSTP>
                <RETIRED_TRAN_TMSTP>9999-12-31 23:59:59.999</RETIRED_TRAN_TMSTP>
                <TRAN_ID>64285012</TRAN_ID>
            </Row>
            <Row rowNumber="3">
                <REL_ID>25372326</REL_ID>
                <EFF_TMSTP>2016-07-08 04:46:02.591</EFF_TMSTP>
                <RETIRED_TRAN_ID>81170279</RETIRED_TRAN_ID>
                <OBJ_ID>13613079</OBJ_ID>
                <REL_OBJ_ID>1350658</REL_OBJ_ID>
                <CUST_VIEW_CD>CORE</CUST_VIEW_CD>
                <CUST_MODEL_CD>CI</CUST_MODEL_CD>
                <TYPE_CD>DRVD</TYPE_CD>
                <UNLINK_FLG>N</UNLINK_FLG>
                <EXPR_TMSTP>9999-12-31 23:59:59.999</EXPR_TMSTP>
                <TRAN_TMSTP>2016-07-08 04:46:02.595</TRAN_TMSTP>
                <RETIRED_TRAN_TMSTP>9999-12-31 23:59:59.999</RETIRED_TRAN_TMSTP>
                <TRAN_ID>81170279</TRAN_ID>
            </Row>
    </ResultSet>
</Results>

From the above response / XML , I have to collect OBJ_ID,REL_OBJ_ID, CUST_MODEL_CD and TYPE_CD tag values from each Row Set as these four values will be passed to the another WebService Request as a parameters.

FYI: I'm using this thru Groovy Scripting in SOAPUI tool.

If you provide the any sample for this, will be helpful for me..

Thanks

2 answers

1

Если XML - документ является действительным , вы можете получить список всех OBJ_ID( в качестве примера) , например , так:

def xml = '''<Results>...</Results>'''
def results = new XmlSlurper().parseText(xml)
results?.ResultSet?.Row?.OBJ_ID.each {
    println(it)
}

Выше код должен принты:

1371718
11983064
13613079

если вы хотите, чтобы собрать их:

List objIds = results?.ResultSet?.Row?.OBJ_ID.collect{it}
println(objIds)

Надеюсь, поможет.

1

Something like this should collect reqiored fields:

def xml = new XmlSlurper().parseText(resultsText)
List<groovy.util.slurpersupport.NodeChild> list =
        xml?.ResultSet?.Row?.collect{it.OBJ_ID + it.REL_OBJ_ID + it.CUST_MODEL_CD + it.TYPE_CD}

Output list.toString():

[[1371718, 1350658, CESE, DRVD], [11983064, 1350658, CESE, DRVD], [13613079, 1350658, CI, DRVD]]

For script Assertion and provided response:

def xml = new XmlSlurper().parseText(messageExchange.response.responseContent)
List<groovy.util.slurpersupport.NodeChild> list =
        xml?.ResultSet?.Row?.collect{it.OBJ_ID + it.REL_OBJ_ID + it.CUST_MODEL_CD + it.TYPE_CD}
assert !(list.isEmpty())        
assert (list.size() == 3)