Reputation: 81
can any one help me how to get tag names in my script.In below code i need to get mbrSqncNum?help me out...
<Id>059A670</healthCardId>
<subscriberId>059A625</subscriberId>
<mbrSqncNum>10</mbrSqncNum>
Upvotes: 1
Views: 4583
Reputation: 171144
Assuming you have valid XML (unlike in your question where there's no root node and mismatched tags), and assuming it's stored in a String variable:
def xml = '''<doc>
| <healthCardId>059A670</healthCardId>
| <subscriberId>059A625</subscriberId>
| <mbrSqncNum>10</mbrSqncNum>
|</doc>'''.stripMargin()
You can then parse this XML using:
def doc = new XmlParser().parseText( xml )
(if it's in a file, you can use this instead)
def doc = new XmlParser().parse( xmlFile )
Now it's unclear from your question exactly what it is you want... To print out all the tag names, you can do:
// prints '[doc, healthCardId, subscriberId, mbrSqncNum]'
println doc.'**'*.name()
(the same thing in longer form would be)
// prints '[doc, healthCardId, subscriberId, mbrSqncNum]'
println doc.breadthFirst()*.name()
Or to get the value of the tag mbrSqncNum
, you can do:
// prints '10'
println doc.mbrSqncNum.text()
Or did you mean something else?
Upvotes: 3