如何获取XML中多次出现的特定标记值 - Groovy脚本?

时间:2018-03-06 11:58:32

标签: xml dom groovy soapui

我有如下的XML内容。

<root>
<customerObjectRelationship>
                       <customerObject>
                          <effectiveTimestamp>2018-03-06T10:17:35.557Z</effectiveTimestamp>
                          <expirationTimestamp>9999-12-31T23:59:59.999Z</expirationTimestamp>
                          <transactionId>68216709</transactionId>
                          <id>12340007</id>
                          <classification>MEM</classification>
                          <type>ACCT</type>
                       </customerObject>
    </customerObjectRelationship>
    <customerObjectRelationship>
                       <customerObject>
                          <effectiveTimestamp>2018-03-06T10:17:28.386Z</effectiveTimestamp>
                          <expirationTimestamp>9999-12-31T23:59:59.999Z</expirationTimestamp>
                          <transactionId>68216647</transactionId>
                          <id>12340005</id>
                          <classification>ENT</classification>
                          <type>FCTY</type>
                       </customerObject>
    </customerObjectRelationship></root>

在上面的XML中,我需要获取 type =&#39; ACCT&#39; id 标记值。类似地,XML中将有多种类型可用。根据类型代码输入,我需要使用Groovy脚本获取相应的值。

我尝试使用以下脚本但无法到达目的地。

def nodes = resultsXml.getDomNodes( "//*:customerObject/*:type" )
for( node in nodes )
{
    def value = com.eviware.soapui.support.xml.XmlUtils.getNodeValue( node )
    log.info (value)
}

上述逻辑还需要包含哪些内容?

感谢。

2 个答案:

答案 0 :(得分:0)

我建议您使用xmlslurperxmlparser

简单代码:

def root = new XmlSlurper().parse(new File ('test.xml'))

root.customerObjectRelationship.customerObject.find{ it.type == 'ACCT' }.id.each{

    println it
}

答案 1 :(得分:0)

我建议在此用例中使用XmlSlurper

解析后,您可以通过findAllcollect方法在感兴趣的节点上应用功能过滤器和地图操作。

def xml = '''<customerObjectRelationship>
               <customerObject>
                 <effectiveTimestamp>2018-03-06T10:17:35.557Z</effectiveTimestamp>
                 <expirationTimestamp>9999-12-31T23:59:59.999Z</expirationTimestamp>
                 <transactionId>68216709</transactionId>
                 <id>12340007</id>
                 <classification>MEM</classification>
                 <type>ACCT</type>
               </customerObject>
             </customerObjectRelationship>
             <customerObjectRelationship>
               <customerObject>
                 <effectiveTimestamp>2018-03-06T10:17:28.386Z</effectiveTimestamp>
                 <expirationTimestamp>9999-12-31T23:59:59.999Z</expirationTimestamp>
                 <transactionId>68216647</transactionId>
                 <id>12340005</id>
                 <classification>ENT</classification>
                 <type>FCTY</type>
                </customerObject>
             </customerObjectRelationship>'''

def resultsXml = new XmlSlurper().parseText("<root>$xml</root>")
def customerObjects = resultsXml.customerObjectRelationship.customerObject

def ids = customerObjects.findAll { it.type.text() == 'ACCT' } // only nodes of type ACCT
                         .collect { it.id.text() } // get the <id> tag value

assert ids == ['123400007']

请注意,对于此示例,我将XML代码段包装在任意<root>标记中,以便它是有效的XML文档,但请使用适合您情况的解析方法。