how to get node values using Xml Parser
Hi , Team
I am trying to read the node values , but getting empty values.
From my SOAP service response i need to read node values.
I got list nodes and each node i need get the values
def parser = new XmlParser(false, true)
def root = parser.parseText(xml)
def BM = root.'**'.findAll{it.name()=='BM'}
log.info BM , // below one is priting
[BM[attributes={}; value=[SymbCd[attributes={}; value=[MCK]], Value[attributes={}; value=[824.00]], VC[attributes={}; value=[-91.15]], VGP[attributes={}; value=[-9.96]], Weight[attributes={}; value=[0.3]]]], BM[attributes={}; value=[SymbCd[attributes={}; value=[OXY]], Value[attributes={}; value=[5005.50]], VC[attributes={}; value=[-521.14]], VGP[attributes={}; value=[-9.43]], Weight[attributes={}; value=[1.88]]]], BM[attributes={}; value=[SymbCd[attributes={}; value=[FANG]], Value[attributes={}; value=[1678.65]], VC[attributes={}; value=[-154.85]], VGP[attributes={}; value=[-8.45]], Weight[attributes={}; value=[0.63]]]], BM[attributes={}; value=[SymbCd[attributes={}; value=[WFC]], Value[attributes={}; value=[4609.44]], VC[attributes={}; value=[-401.94]], VGP[attributes={}; value=[-8.02]], Weight[attributes={}; value=[1.73]]]], BM[attributes={}; value=[SymbCd[attributes={}; value=[MLM]], Value[attributes={}; value=[1747.60]], VC[attributes={}; value=[-140.70]], VGP[attributes={}; value=[-7.45]], Weight[attributes={}; value=[0.65]]]]]
Thanks
Babu
log.info BM.children().find( {it.name() == "A"})?.text();
Even more elegant and precise one would be:
//Closure to show the BM node details def showBM = { BM, index -> log.info "Detail of BM node ${index}"
//Print of log - either one
//BM.children().each { println "${it.name()} : $it" } BM.children().each { log.info "${it.name()} : $it" } }Of course, rest of the script is same.