SoapUI使用groovy脚本从URL解析XML

时间:2014-09-09 18:21:57

标签: xml groovy singleton datasource soapui

我正在使用SoapUI pro Datasource groovy脚本:

import groovy.util.slurpersupport.GPathResult

@Singleton 
class UrlXmlParser {

    //def url = "http://www.xmlfiles.com/examples/plant_catalog.xml"
    def slurper  = new XmlSlurper()

    GPathResult parse(String url){
//      def xmlText = url.toURL().text
//      def content = slurper.parseText(xmlText)
        slurper.parse(url)
    }
}

def allPlants = UrlXmlParser.parse("http://www.xmlfiles.com/examples/plant_catalog.xml").PLANT

//
//log.info "it works"+allPlants.size()

def row = testRunner.testCase.testSteps["groovyDS"].currentRow

if(row < allPlants.size()){
    result["zone"]=content.PLANT[row].ZONE
    result["light"]=content.PLANT[row].LIGHT
    result["price"]=content.PLANT[row].PRICE

}

但是因为GPathResult而导致错误:

No signature of method: static UrlXmlParser.parse() is applicable for
argument types: (java.lang.String) values:
[http://www.xmlfiles.com/examples/plant_catalog.xml] Possible
solutions: parse(java.lang.String), use([Ljava.lang.Object;), wait(),
grep(), any(), wait(long)

1 个答案:

答案 0 :(得分:0)

找到了答案

import groovy.util.slurpersupport.GPathResult

@Singleton 
class UrlXmlParser {

    def slurper  = new XmlSlurper()

    GPathResult parse(String url){
//      def xmlText = url.toURL().text
//      def content = slurper.parseText(xmlText)
        slurper.parse(url)
    }
}

def url = "http://www.xmlfiles.com/examples/plant_catalog.xml"
def allPlants = UrlXmlParser.instance.parse(url).PLANT



def row = testRunner.testCase.testSteps["groovyDS"].currentRow

if(row < allPlants.size()){
    result["zone"]=content.PLANT[row].ZONE
    result["light"]=content.PLANT[row].LIGHT
    result["price"]=content.PLANT[row].PRICE

}