我可以使用Groovy和XmlSlurper通过HTTP URL解析XML并输出与标记关联的值。下面是生成的示例XML页面。
<worklog>
<worklog_id>10100</worklog_id>
<issue_key>TST-3</issue_key>
<hours>2.0</hours>
<work_date>2014-01-01</work_date>
<work_date_time>2014-01-01 00:00:00</work_date_time>
<username>admin</username>
<reporter>admin</reporter>
</worklog>
<worklog>
<worklog_id>10200</worklog_id>
<issue_key>TST-3</issue_key>
<work_date>2014-01-02</work_date>
<work_date_time>2014-01-02 00:00:00</work_date_time>
<username>admin</username>
<reporter>admin</reporter>
</worklog>
<worklog>
<worklog_id>10201</worklog_id>
<issue_key>TST-3</issue_key>
<hours>0.25</hours>
<work_date>2014-01-02</work_date>
<work_date_time>2014-01-02 10:33:00</work_date_time>
<username>admin</username>
<reporter>admin</reporter>
</worklog>
<worklog>
<worklog_id>10400</worklog_id>
<issue_key>TST-3</issue_key>
<hours>2.0</hours>
<work_date>2014-01-07</work_date>
<work_date_time>2014-01-07 12:03:00</work_date_time>
<username>admin</username>
<reporter>admin</reporter>
</worklog>
<worklog>
<worklog_id>10202</worklog_id>
<issue_key>TST-4</issue_key>
<hours>1.0</hours>
<work_date>2014-01-02</work_date>
<work_date_time>2014-01-02 15:52:00</work_date_time>
<username>admin</username>
<reporter>admin</reporter>
</worklog>
但是,在我的XML中,我需要遍历我的XML并查找具有相同值的issue_key标记。如果有多个具有相同值的issue_key标记,如此处的“TST-3”, 那么我想收集和巩固与小时work_date,work_date_time,用户名,ACTIVITY_NAME,work_description,parent_key,记者标签为这个单一issue_key标签关联的值,然后将其用具有其他标签一起生成的输出也以相同的顺序不同的issue_key值。
很抱歉成为Groovy和XmlSlurper的完整菜鸟。但任何人都可以告诉我如何去做。另外,下面是我的Groovy代码,用于从XML获取属性。
def worklogList = new ArrayList<Worklog>()
def wklog
def worklogs = new XmlSlurper().parse(new File("C:\\xml-worklog\\worklog.xml"))
worklogs.worklog.each {node ->
wklog = new Worklog();
wklog.work_date = node.work_date
wklog.work_date_time = node.work_date_time
wklog.issue_key = node.issue_key
wklog.hours = node.hours
wklog.username = node.username
wklog.reporter = node.reporter
worklogList.add(wklog)
}
worklogList.each {wklogT -> println(wklogT)}
}
class Worklog
{
String issue_key
String hours
String work_date
String work_date_time
String username
String activity_name
String work_descripton
String reporter
@Override
public String toString()
{
return "Issue Key: ${issue_key} \t Hours: ${hours} \t Work Date: ${work_date} \t Work Date Time: ${work_date_time} \t Username: ${username} \t Reporter: ${reporter} \t Activity Name: ${activity_name} \t Description: ${work_descripton}"
}
}
特定问题密钥的预期o / p,假设'TST-3'是 -
工作小时数|工作日期|工作日期时间|用户名|记者|
2.0 | 2014-01-01 | 2014-01-01 00:00:00 |管理员| admin |
| 2014-01-02 | 2014-01-02 00:00:00 | admin | admin |
0.25 | 2014-01-02 | 2014-01-02 10:33:00 |管理员| admin |
2.0 | 2014-01-07 | 2014-01-02 12:03:00 |管理员|管理员|
如何在每个issue_key属性的有序列表对象中获取这些值。
答案 0 :(得分:3)
作为解决问题的快速且可转发的方法,我建议您创建一个工厂方法,它将通过问题密钥为您提供初始化Worklog
实例。
在这种情况下,您可以像处理一样处理xml,只需稍作更改即可。以下是处理工作日志的代码草案。
def worklogs = [:]
def createWorklog(String id) {
if (!worklogs.containsKey(id))
worklogs[id] = new Worklog()
return worklogs[id]
}
worklogs.worklog.each {node ->
wklog = createWorklog(node.issue_key); // creates, or give created, and save it to list.
// It don't know, what do you want to do with different dates or usernames.
// If you want, you can have a list of them, and add value to list here, or consolidate whole change structures to list.
wklog.hours += node.hours // aggregate hours.
}
答案 1 :(得分:1)
或者您可以定义plus
方法来添加Worklog
个实例:
@groovy.transform.Canonical
@groovy.transform.ToString
class Worklog
{
String issue_key
Double hours
String work_date
String work_date_time
String username
String activity_name
String work_description
String reporter
Worklog plus( Worklog w ) {
new Worklog( issue_key,
hours + w.hours,
work_date,
work_date_time,
username,
activity_name,
work_description,
reporter )
}
}
然后解析xml,按issue_key
对条目进行分组,并将这些组添加到一个条目中:
def log = new XmlSlurper().parse( xmlFile )
.worklog
.collect { w ->
new Worklog( w.issue_key.text(),
w.hours.text() ? w.hours.text().toDouble() : 0,
w.work_date.text(),
w.work_date_time.text(),
w.username.text(),
w.activity_name.text(),
w.work_description.text(),
w.reporter.text() )
}
.groupBy { it.issue_key }
.values()*.inject { a, b -> a + b }
答案 2 :(得分:0)
我在soapui中尝试了以下内容,创建了新的groovy步骤并添加了下面的代码,
更短的版本:
def publishbooks = new 的XmlSlurper()。分析( 'http://xyz:8003/myflow/api/books?expanded=true')
log.info publishbooks.publishJob [0] .jobName
详细版本:
//Written by Raja. // This code will parse the uri - http://xyz:8003/flow/api/jobs?expanded=true // and iterate through publishJob nodes and gets the jobid relavant to jobname we added. def publishJobs = new XmlSlurper().parse('http://xyz:8003/flow/api/jobs?expanded=true') String TSjobname = testRunner.testCase.testSuite.getPropertyValue('jobname') publishJobs.publishJob.findAll { it.jobName == TSjobname }.each { String id = it.@id testRunner.testCase.testSuite.setPropertyValue( "jobid", id ) log.info id; }
uri中的XML内容 - http://xyz:8003/myflow/api/books?expanded=true是
<PublishJobs>
<publishJob id="248" xlink:href="http://xyz:8003/myflow/api/books/248">
<completedTasksCount>196</completedTasksCount>
<tasksCount>196</tasksCount>
<generateArchive>false</generateArchive>
<jobName>11111</jobName>
<priority>10</priority>
<status>FINISHED</status>
</publishJob>
<publishJob id="250" xlink:href="http://xyz:8003/myflow/api/books/250">
<completedTasksCount>51</completedTasksCount>
<tasksCount>51</tasksCount>
<generateArchive>false</generateArchive>
<jobName>44444</jobName>
<priority>10</priority>
<status>FINISHED</status>
</publishJob>
</PublishJobs>