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

How do I fetch the particular tag value that is present multiple times in the XML - Groovy Script?

我有 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中,我需要获取id标签值type='ACCT' .同样,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)
}

以上逻辑还需要包含什么?

谢谢。

我建议你使用xmlslurperxmlparser

简单代码:

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

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

    println it
}

我建议在此用例中使用 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 文档,但使用适合的解析方法你的情况。