-2

How can I get the value of any tag (e.g. Quantity) from the below XML by passing the tag name without using hard coded tag name-

def temp1="""
<TradeRecord>
   <TradeId>1000</TradeId> 
   <SecurityId>10382456</SecurityId> 
   <TradeType>SELLL</TradeType> 
   <TradeDate>2014-03-21</TradeDate> 
   <Broker>100</Broker> 
   <Exchange>1</Exchange> 
   <Status>INIT</Status> 
   <Quantity>125</Quantity> 
   <ApprovedBy /> 
</TradeRecord>
"""

def records = new XmlParser().parseText(temp1)
//log.info records.Quantity[0].text()     By using this i am getting value but i want     'Quantity' to come from a string
tag = 'Quantity'
xy = records["Quantity"].value;   'This is not working
log.info xy
Opal
  • 81,889
  • 28
  • 189
  • 210

2 Answers2

0

You should be able to do

records."$tag".text()
tim_yates
  • 167,322
  • 27
  • 342
  • 338
0

You should have used .text() method

records[tag].text()

kdabir
  • 9,623
  • 3
  • 43
  • 45
  • Thanks Kunal, my one more query how can i set value in any of these tag which again comes from a string. – user3774346 Jul 12 '14 at 09:35
  • May be this is what you are looking for : http://stackoverflow.com/questions/16061762/xmlslurper-how-to-change-the-text-of-a-dynamic-node – kdabir Jul 12 '14 at 18:44
  • Kunal, that code is not working for me.I want to update any tag value but that tag name and new value comes from a csv. – user3774346 Jul 13 '14 at 06:18