当父标记属性值是唯一的时,如何获取父标记的标记值?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了当父标记属性值是唯一的时,如何获取父标记的标记值?相关的知识,希望对你有一定的参考价值。
我有这样的SOAP XML数据。
<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>
从上面的响应/ XML中,我必须从每个行集中收集OBJ_ID,REL_OBJ_ID,CUST_MODEL_CD和TYPE_CD标记值,因为这四个值将作为参数传递给另一个WebService请求。
仅供参考:我在SOAPUI工具中使用Groovy Scripting。
如果您为此提供任何样品,将对我有所帮助..
谢谢
答案
如果您的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)
希望能帮助到你。
另一答案
这样的东西应该收集必填字段:
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}
输出list.toString()
:
[[1371718, 1350658, CESE, DRVD], [11983064, 1350658, CESE, DRVD], [13613079, 1350658, CI, DRVD]]
对于脚本断言和提供的响应:
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)
以上是关于当父标记属性值是唯一的时,如何获取父标记的标记值?的主要内容,如果未能解决你的问题,请参考以下文章
如何将父控件标记值传递给 ParameterConverter [重复]