如何遍历xml节点并检查属性值?

时间:2011-05-17 06:44:35

标签: xml groovy xmlslurper

我有这个xml,我把它带到了xml a GPathResult object我怎么能得到Microsoft(R) Windows(R) Server 2003 Enterprise x64 Edition基于使用groovy xml slurping检查fieldOS 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

这是快照 enter image description here

这是我遍历后解决的问题

      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

1 个答案:

答案 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()}"
}