Forum Discussion

babusr01's avatar
babusr01
Contributor
8 years ago
Solved

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.

11 Replies

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

      Hi

       

      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="http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Body>
      <ns2:GDR xmlns:ns2="URL" xmlns:ns3="URL">
      <PAM>
      <TP>
      <A>MSFT</A>
      <V>9316.84</V>
      <VC>-177.12</VC>
      <VCP>-1.87</VCP>
      <W>3.5</W>
      </TP>
      <TP>
      <A>AAPL</A>
      <V>8517.75</V>
      <VC>567.75</VC>
      <VCP>7.14</VCP>
      <W>3.2</W>
      </TP>
      <TP>
      <A>GOOG</A>
      <V>6942.69</V>
      <VC>20.88</VC>
      <VCP>0.3</VCP>
      <W>2.61</W>
      </TP>
      <BM>
      <A>MCK</A>
      <V>824.00</V>
      <VC>-91.15</VC>
      <VCP>-9.96</VCP>
      <W>0.3</W>
      </BM>
      <BM>
      <A>OXY</A>
      <V>5005.50</V>
      <VC>-521.14</VC>
      <VCP>-9.43</VCP>
      <W>1.88</W>
      </BM>
      <BM>
      <A>FANG</A>
      <V>1678.65</V>
      <VC>-154.85</VC>
      <VCP>-8.45</VCP>
      <W>0.63</W>
      </BM>
      </PAM>
      </ns2:GDR>
      </soap:Body>
      </soap:Envelope>

      • nmrao's avatar
        nmrao
        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?