0

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
AabinGunz
  • 12,109
  • 54
  • 146
  • 218
  • Do you have the code you used to parse the xml into the `gpathResult` variable? – tim_yates May 17 '11 at 08:40
  • @tim_yates: ya i have, i'll post it in a minute – AabinGunz May 17 '11 at 08:43
  • How is the `gpathResult` created (`XmlSlurper`, etc?)....that was my question... – tim_yates May 17 '11 at 08:49
  • @tim : check now, file path is correct as that is how i get other stuff printed onscreen – AabinGunz May 17 '11 at 08:52
  • @tim_yates: I was thinking may be i can traverse through all the osinfo, and then check for `field` name if present then print that particular `information` attribute. So how can i traverse this osinfo and check for field attribs/ – AabinGunz May 17 '11 at 08:56

1 Answers1

3

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()}"
}
tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • HI 1 prints correctly, but HI 2 still does not print anything, and in my system its `environment` instead of `env`. Also i am using groovy 1.8, i'll have a snapshot in a minute , check my question – AabinGunz May 17 '11 at 09:11
  • @Abhishek It must be something you're not telling us then. As the XML and code do not match, I am guessing that something is wrong with either of them, and you are not posting the correct information here for us to help you. Was that with you running **exactly** the code above that prints nothing for `HI 2`? – tim_yates May 17 '11 at 09:13
  • ok just tell me how to traverse through each environment.variable elements, so i can try something else, may be that works – AabinGunz May 17 '11 at 09:14
  • i copy pasted your `println` statement and here i posted just a bit of the xml, i dont want to reveal parts of xml and node names are also diff, but i am being cautious while editing, that is why env was environment by mistake – AabinGunz May 17 '11 at 09:16
  • I think the problem must be that you have `OSinfo` or `osInfo` or something, not `osinfo` as you posted here – tim_yates May 17 '11 at 09:26
  • everything is in small caps, i am sure, please tell me how to traverse through each `environment.variable` elements, so i can try something else – AabinGunz May 17 '11 at 09:32
  • Added an example of iterating through the nodes with `each` to the bottom of my answer – tim_yates May 17 '11 at 09:51