Forum Discussion

babusr01's avatar
8 years ago

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{'BM'} 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]]]]]





  • BM.children().find( { == "A"})?.text();

  • Even more elegant and precise one would be:


    //Closure to show the BM node details
    def showBM = { BM, index -> "Detail of BM node ${index}"
    //Print of log - either one
    //BM.children().each { println "${} : $it" } BM.children().each { "${} : $it" } }

    Of course, rest of the script is same.

11 Replies

  • nmrao's avatar
    Champion Level 3
    And mention which data you wanted to extract.
    • babusr01's avatar



      Please find the response, i want to read the values from BM section it have 3 collection and also same from TP.


      <soap:Envelope xmlns:soap="">
      <ns2:GDR xmlns:ns2="URL" xmlns:ns3="URL">

      • nmrao's avatar
        Champion Level 3
        What is the issue in the code that you posted? That is already showing the data correctly.
        Do you want to do any further processing after getting BM collection?