通过在Groovy中的XML Parser方法中从字符串传递Tag名称来获取Tag值

时间:2014-07-12 08:01:05

标签: groovy xml-parsing

如何通过传递标记名称而不使用硬编码标记名称从以下XML获取任何标记的值(例如Quantity) -

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

2 个答案:

答案 0 :(得分:0)

你应该能够做到

records."$tag".text()

答案 1 :(得分:0)

您应该使用.text()方法

records[tag].text()