2011-05-17 95 views
0

我有這樣的XML,我把它在XML a GPathResult object我怎樣才能Microsoft(R) Windows(R) Server 2003 Enterprise x64 Edition基於使用常規XML啜如何遍歷XML節點並檢查屬性值?

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

這裏與field值檢查爲OS Name被解析代碼

def file = new File(filepath) 
def gpathResult = new XmlSlurper().parse(file) 

[email protected]() 
      log.info [email protected]() 


      System.out.println("HI 1"[email protected]()); 
      System.out.println("HI 2"[email protected]()); 

      if([email protected]().equals("OS")) 
      { 
       [email protected]ext() 

      } 
      if([email protected]().equals("COMPUTERNAME")) 
      { 
       [email protected].text() 
      } 

這裏HI 1打印所有的環境名稱屬性值,但HI 2只打印HI 2

這裏是快照 enter image description here

這裏是解決後,我走過

 def vals1=gpathResult.machine.env.variable.findAll{[email protected]=='COMPUTERNAME'}[email protected]() 
      println vals1 
      csmSummary.hostname=vals1 
      def vals2=gpathResult.machine.env.variable.findAll{[email protected]=='OS'}[email protected]() 
      println vals2 
      csmSummary.osname=vals2 
+0

你有你所使用的XML解析成'gpathResult'變量的代碼? – 2011-05-17 08:40:19

+0

@tim_yates:我有,我會在一分鐘內發佈它 – abi1964 2011-05-17 08:43:13

+0

@tim:我編輯我的問題 – abi1964 2011-05-17 08:48:05

回答

2

奇怪......如果我這樣做(用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 ${[email protected]*.text()}" 
println "HI 2 ${[email protected]*.text()}" 

它打印出:

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

你可以試試該代碼(假設你是你星1.8,Groovy的最新版本 - 如果不是,你將需要使用""",而不是$/的字符串分隔符,而逃避\字符)

[編輯]這可能只是因爲你正在使用,而不是gpathResult.machine.environment.variablegpathResult.machine.env.variable

遍歷ENV節點,你會做這樣的事情:

gpathResult.machine.env.variable.each { node -> 
    println "${[email protected]()} contains ${[email protected]()}" 
} 
+0

HI 1正確打印,但是HI 2仍然不打印任何東西,而在我的系統中它的'environment'而不是'env'。此外,我正在使用groovy 1.8,我會在一分鐘快照,檢查我的問題 – abi1964 2011-05-17 09:11:21

+0

@Abhishek它一定是你不告訴我們的東西。由於XML和代碼不匹配,我猜測其中任何一個都有問題,並且您沒有在此發佈正確的信息以幫助我們。是否與**一樣運行**上面的代碼不會爲'HI 2'打印任何內容? – 2011-05-17 09:13:22

+0

好吧只是告訴我如何遍歷每個environment.variable的元素,所以我可以嘗試別的,可能是作品 – abi1964 2011-05-17 09:14:35

相關問題