我有这个xml,我把它带到了xml a GPathResult object
我怎么能得到Microsoft(R) Windows(R) Server 2003 Enterprise x64 Edition
基于使用groovy xml slurping检查field
值OS Name
1} p>
<client productname="abp">
<machine>
<env>
<variable name='ALLUSERSPROFILE' value='C:\Documents and Settings\All Users' />
<variable name='APPDATA' value='C:\Documents and Settings\Administrator\Application Data' />
<variable name='OS' value='Windows_NT' />
<variable name='COMPUTERNAME' value='AbhishekPC' />
</env>
<osinfo>
<osinfo field='OS Name' information='Microsoft(R) Windows(R) Server 2003 Enterprise x64 Edition' />
<osinfo field='OS Version' information='5.2.3790 Service Pack 2 Build 3790' />
<osinfo field='OS Manufacturer' information='Microsoft Corporation' />
<osinfo field='OS Configuration' information='Standalone Server' />
<osinfo field='OS Build Type' information='Multiprocessor Free' />
</osinfo>
</machine>
</client>
这是解析代码
def file = new File(filepath)
def gpathResult = new XmlSlurper().parse(file)
summary.productname=gpathResult.@product.text()
log.info gpathResult.system.osinfo.osinfo.@field.text()
System.out.println("HI 1"+gpathResult.machine.environment.variable.@name.text());
System.out.println("HI 2"+gpathResult.machine.osinfo.osinfo.@field.text());
if(gpathResult.machine.environment.variable.@name.text().equals("OS"))
{
summary.osname=gpathResult.machine.environment.variable.@value.text()
}
if(gpathResult.machine.environment.variable.@name.text().equals("COMPUTERNAME"))
{
summary.hostname=gpathResult.machine.environment.variable.@value.text()
}
此处HI 1
打印所有环境名称属性值,但HI 2
仅打印HI 2
这是快照
这是我遍历后解决的问题
def vals1=gpathResult.machine.env.variable.findAll{it.@name=='COMPUTERNAME'}.@value.text()
println vals1
csmSummary.hostname=vals1
def vals2=gpathResult.machine.env.variable.findAll{it.@name=='OS'}.@value.text()
println vals2
csmSummary.osname=vals2
答案 0 :(得分:3)
奇怪......如果我这样做(使用Groovy 1.8)
def gpathResult = new XmlSlurper().parseText( $/<client productname="abp">
<machine>
<env>
<variable name='ALLUSERSPROFILE' value='C:\Documents and Settings\All Users' />
<variable name='APPDATA' value='C:\Documents and Settings\Administrator\Application Data' />
<variable name='OS' value='Windows_NT' />
<variable name='COMPUTERNAME' value='AbhishekPC' />
</env>
<osinfo>
<osinfo field='OS Name' information='Microsoft(R) Windows(R) Server 2003 Enterprise x64 Edition' />
<osinfo field='OS Version' information='5.2.3790 Service Pack 2 Build 3790' />
<osinfo field='OS Manufacturer' information='Microsoft Corporation' />
<osinfo field='OS Configuration' information='Standalone Server' />
<osinfo field='OS Build Type' information='Multiprocessor Free' />
</osinfo>
</machine>
</client>/$ )
println "HI 1 ${gpathResult.machine.env.variable.@name*.text()}"
println "HI 2 ${gpathResult.machine.osinfo.osinfo.@field*.text()}"
打印出来:
HI 1 [ALLUSERSPROFILE, APPDATA, OS, COMPUTERNAME]
HI 2 [OS Name, OS Version, OS Manufacturer, OS Configuration, OS Build Type]
你可以试试那段代码(假设你使用的是1.8,Groovy的最新版本 - 如果没有,你需要使用"""
代替$/
作为字符串分隔符,并逃避\
字符)
[edit]这可能只是因为您使用的是gpathResult.machine.environment.variable
而不是gpathResult.machine.env.variable
遍历env节点,你会做类似的事情:
gpathResult.machine.env.variable.each { node ->
println "${node.@name.text()} contains ${node.@value.text()}"
}