如何使用 groovy 脚本获取特定的 xml 节点值

How to get a particular xml node values using groovy script

这是我的肥皂回复。我正在使用免费版本的 soapui。 有人请帮助我从 ** ** 节点使用 groovy

获取值 60,61 作为数组
            <tRiseMedia:StreamName>VideoEncoder_Encoder1</tRiseMedia:StreamName>
            <tRiseMedia:OSDConfiguration token="61"> 
             <tt:VideoSourceConfigurationToken>VideoSource_VisibleCamera</tt:VideoSourceConfigurationToken>           
              </tRiseMedia:OSDConfiguration>
         </tRiseMedia:StreamOsdList>     

这是我的 groovy 脚本片段

import com.eviware.soapui.support.XmlHolder
import com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext
def TestCase = context.testCase
PropertiesTestStep = TestCase.getTestStepByName("Properties")
def response = testRunner.testCase.getTestStepByName('GetStreamOSDs').getPropertyValue("response")
def samplexmlreq=new XmlHolder(response)

def tokenFromResponse = samplexmlreq.getNodeValue("/*:OSDConfiguration/@token")


def envelope = new  XmlParser(false, false).parse(samplexmlreq)
def tokens = envelope.'**'.findAll { node -> node.name() == 'tRiseMedia:OSDConfiguration' }*.@token
log.info  token

如果你只想要一个标记列表,你可以这样做:

def xmlText = '''<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
   <SOAP-ENV:Header/>
   <SOAP-ENV:Body>
      <tRiseMedia:GetStreamOSDsResponse>
         <tRiseMedia:StreamOsdList>
            <tRiseMedia:StreamName>VideoEncoder_Encoder1</tRiseMedia:StreamName>
            <tRiseMedia:OSDConfiguration token="60"> 
              <tt:VideoSourceConfigurationToken>VideoSource_VisibleCamera</tt:VideoSourceConfigurationToken>
               <tt:Type>Text</tt:Type>
               <tt:Position>
                  <tt:Type>Custom</tt:Type>
                  <tt:Pos x="15" y="50"/>
               </tt:Position>
               <tt:TextString>
                  <tt:Type>Sector</tt:Type>                  
               </tt:TextString>
            </tRiseMedia:OSDConfiguration>
         </tRiseMedia:StreamOsdList>
         <tRiseMedia:StreamOsdList>
            <tRiseMedia:StreamName>VideoEncoder_Encoder1</tRiseMedia:StreamName>
            <tRiseMedia:OSDConfiguration token="61"> 
             <tt:VideoSourceConfigurationToken>VideoSource_VisibleCamera</tt:VideoSourceConfigurationToken>
               <tt:Type>Text</tt:Type>
               <tt:Position>
                  <tt:Type>Custom</tt:Type>
                  <tt:Pos x="35" y="35"/>
               </tt:Position>
               <tt:TextString>
                  <tt:Type>Sector</tt:Type>                  
               </tt:TextString>
            </tRiseMedia:OSDConfiguration>
         </tRiseMedia:StreamOsdList>     
      </tRiseMedia:GetStreamOSDsResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
'''

def envelope = new  XmlParser(false, false).parseText(xmlText)

def tokens = envelope.'**'.findAll { node -> node.name() == 'tRiseMedia:OSDConfiguration' }*.@token

assert tokens == ['60', '61']

您可以获得这样的响应 XML:

import com.eviware.soapui.support.XmlHolder
def xmlText = new XmlHolder(context.response)

在 SoapUI 中,您将使用 parse 而不是 parseTextparseText 用于此示例,因为 XML 字符串在源代码中作为文本。

我得到的解决方案如下

import com.eviware.soapui.support.XmlHolder
import com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext
def TestCase = context.testCase
PropertiesTestStep = TestCase.getTestStepByName("Properties")
def response = testRunner.testCase.getTestStepByName('GetStreamOSDs').getPropertyValue("response")

def envelope = new  XmlParser().parseText(response)
//Getting all the token values from the response
def tokens = envelope.'**'.findAll { node -> node.name() == 'tRiseMedia:OSDConfiguration' }*.@token
log.info tokens