Forum Discussion

Scripter's avatar
Scripter
Occasional Contributor
9 years ago

Extracting value of an attribute from XML using groovy

I would use same sample xml from my previous post

 

 <property name="mail">	
       <entry key="server" value="outlook"/>
  </property>
<property name="housekeep">
	<entry key="files.days" value="60"/>
	<entry key="logs.days" value="30"/>
</property>
<property name="ConnectedDB">
	<entry key="name" value="oracle" />
	<entry key="table1" value="tableA" />
	<entry key="table2" value="tableB"/> 
</property>

Using groovy, I have to access "oracle". I am hard-coding to get the value of oracle. ..@value[0]

 

def getxml = new XmlSlurper().parse(new File ('C:/ConfigXML/file.xml'))
def chosenDB =getxml.property.find{it.@name=='ConnectedDB'}.entry.@value[0]
	log.info(chosenDB) //gives oracle

  Pls suggest without the hard-coded way..like give me the 'value' where 'key'== 'name' from property 'ConnectedDB'

 

 

No RepliesBe the first to reply