Groovy从xml文件中提取值

时间:2015-11-12 05:19:36

标签: xml groovy

我有soapui而我正在尝试从XML文件中提取false值。

responseCurrentDate.xml

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns="https://alo.io/ns/20130518">
   <soap:Body>
      <log_resp>
         <alo_env>
            <cid>K06BZQ6476WHP5ZFWW0NEJC937GB</cid>
            <result>ALO_OK</result>
         </alo_env>
         <item>
            <response>{"timestamp": "2015-11-12T03:02:45Z", "data": null, "success": false, "error": {"message": "{'desc': \"Can't contact LDAP server\"}", "code": 500, "label": "Internal Server Error"}}</response>
         </item>
      </log_resp>
   </soap:Body>
</soap:Envelope>

我用groovy

尝试了这个
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )

def date = new Date()
def dts = date.format("yyy-MM-dd-HH-mm-ss")

def myOutFile = "C:/temp2/response"+dts+".xml"
def response = context.expand('${login#Response}')
def f = new File(myOutFile)
f.write(response, "UTF-8")

def xmlfile = myOutFile 
def x = new XmlParser(),parseText(myOutFile)
println it.response.item.success

我得到了他的错误:

  

org.xml.sax,SAXException.lineNumber1mcolumnNumber1,Prolog中不允许使用内容

1 个答案:

答案 0 :(得分:1)

正如tim_yates所指出的,不要将xml内容保存在文件中,只需将parseText与response一起使用:

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )

def date = new Date()
def dts = date.format("yyy-MM-dd-HH-mm-ss")

def response = context.expand('${login#Response}')
def x = new XmlParser().parseText(response)

但是,如果您确实需要将内容保存在文件中,而不是使用parse而不是parseTextFile实例,而不是使用包含文件名的字符串:

def f = new File(myOutFile)
f.write(response, "UTF-8")    
def x = new XmlParser().parse(f)