我有这个 xml,我把它放在 xml中,我a GPathResult object
怎样才能得到Microsoft(R) Windows(R) Server 2003 Enterprise x64 Edition
基于使用 groovy xml slurping的field
值 检查OS Name
<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