Question

I have this xml, i took it in xml a GPathResult object how can I get Microsoft(R) Windows(R) Server 2003 Enterprise x64 Edition based on checking with the field value as OS Name using groovy xml slurping

<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>

Here is the parsing code

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()
            }

Here HI 1 prints all the environments name attribut values but HI 2 only prints HI 2

here is the snapshot enter image description here

here is what solved after i traversed

      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
Was it helpful?

Solution

Strange... if I do this (with 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()}"

it prints out:

HI 1 [ALLUSERSPROFILE, APPDATA, OS, COMPUTERNAME]
HI 2 [OS Name, OS Version, OS Manufacturer, OS Configuration, OS Build Type]

Can you try that code (assuming you are using 1.8, the latest version of Groovy -- if not, you will need to use """ instead of $/ for the string delimiters, and escape the \ chars)

[edit] It's probably just because you are using gpathResult.machine.environment.variable instead of gpathResult.machine.env.variable

to traverse the env nodes, you'd do something like:

gpathResult.machine.env.variable.each { node ->
  println "${node.@name.text()} contains ${node.@value.text()}"
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top