2015-11-12 50 views
2

我想從文本文件中提取一行中的令牌值。使用groovy從文本文件中提取一行中的提取值

從文本文件中的行是:

<response>{"timestamp": "2013-11-12T14:55:43Z", "data": {"profile": null, "ticket": "f644231-6d46-44c7-add6-de3a4422871e", </response> 

常規文件是:

// read the file from path 
    def file = new File('c:/tmp/response.txt') 
    // for example read line by line 
    def data= file.eachLine { line -> 
     // check if the line contains your data 
     if(line.contains('"ticket":')){ 
      return line  
     } 
    } 
testRunner.testCase.testSuite.project.setPropertyValue('ticket',data) 

所以在這裏它不是存儲在我的票可變的任意值。 任何幫助,請

+0

文件是否有JSON文本?如果是的話,你可以使用JsonSlurper –

+0

這個文件是包含json格式的xml,但我轉換爲txt文件 –

回答

2

更換

def data= file.eachLine { line -> 
    // check if the line contains your data 
    if(line.contains('"ticket":')){ 
     return line  
    } 
} 

隨着

def data= file.filterLine { line -> 
    line.contains('"ticket":') 
} 
+1

謝謝Tim_yates,用你的代碼,它顯示包含數據的所有行 –

+0

最後,我找到了一個帶點擊功能的解決方案它爲我們正在尋找的節點生成自己的groovy。 –