【问题标题】:How to get a particular xml node values using groovy script如何使用 groovy 脚本获取特定的 xml 节点值
【发布时间】:2021-11-29 06:51:44
【问题描述】:

这是我的肥皂回复。我正在使用免费版的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 脚本 sn-p

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

【问题讨论】:

    标签: java groovy soap wsdl soapui


    【解决方案1】:

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

    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 字符串在源中作为文本。

    【讨论】:

    • Gettig 错误,例如“groovy.lang.MissingMethodException:没有方法签名:groovy.util.XmlParser.parseText() 适用于参数类型:(com.eviware.soapui.support.XmlHolder)values : [ ] 可能的解决方案:parseText(java.lang.String), parse(java.io.File), parse(java.io.InputStream), parse(java.io.Reader), parse(java.lang.String) , 解析(org.xml.sax.InputSource)"
    • 尝试将.parseText 更改为.parse。我更新了答案。
    • 还是有这个错误。
    • groovy.lang.MissingMethodException:没有方法签名:groovy.util.XmlParser.parse() 适用于参数类型:(com.eviware.soapui.support.XmlHolder) 值:[] 可能解决方案:parse(java.io.File), parse(java.io.InputStream), parse(java.io.Reader), parse(java.lang.String), parse(org.xml.sax.InputSource), 使用([Ljava.lang.Object;)
    • 你必须用你的代码更新你的问题。
    【解决方案2】:

    我得到了如下解决方案

    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
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-30
      • 1970-01-01
      • 2021-09-14
      相关资源
      最近更新 更多