Forum Discussion

zgh4693's avatar
zgh4693
Occasional Contributor
2 years ago
Solved

how to get xml joint value and output in groovy

i have a xml 

'''<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://abc" >
    <Cool Type="abc" >
    </Cool>
    <Cool Type="abk" >
<Line LineID="0" Main="1"> <Info updateDate="17-05-2028" > <Selection Tag="A" Status="Offer"/> <Selection Tag="A" Status="Offer"/> <Selection Tag="A" Status="Offer"/> </Info> </Line>
</Cool> <Cool Type="abg" >
<Line LineID="0" Main="1"> <Info updateDate="17-05-2028" > <Selection Tag="C" Status="Offer"/> <Selection Tag="B" Status="Offer"/> <Selection Tag="B" Status="Offer"/> </Info> </Line>
</Cool> <Cool Type="abg" > <Line LineID="0" Main="1"> <Info updateDate="17-05-2028" > <Selection Tag="A" Status="Offer"/> <Selection Tag="A" Status="Offer"/> <Selection Tag="B" Status="Offer"/> </Info> </Line> </Cool> <Cool Type="abg" > <Line LineID="0" Main="1"> <Info updateDate="17-05-2028" > <Selection Tag="A" Status="Offer"/> <Selection Tag="A" Status="Offer"/> <Selection Tag="A" Status="Offer"/> </Info> </Line> </Cool> </SOAP-ENV:Envelope> '''

 I want to get Cool Type ='abg' and Selection Tag="A" then output value ='A'  what should i do? 

 I try to using this script but only can get Type='abg' or Tag='A'  I want Joint these Attributes then output attributes 

def res = '''
''' xml = new XmlParser().parseText(res) results = xml.'**'.findAll { it.@Type == 'abg' } results.each { log.info(it.@Type) }

 I want to log.info (@Tag )  

I try to results = xml.'**'.findAll { it.@Type == 'abg' && it.@Tag== 'A'}

       resuluts.each{

             log.info(it.@Tag)

}

but it only log @Tag=A, No log @Type==abg > @Tag=A  *-*

  • Please see if this is what you need?

     

     

     

    def xml = new XmlParser().parseText(str)
    def result = xml.'**'.findAll{it.@Type == 'abg'}
    result.each { cool ->
    	cool.'**'.findAll {it.@Tag == 'A'}.each {
    		log.info "Type = ${cool.@Type} and Tag = ${it.@Tag}"   	
    	}
    }

     

     

10 Replies

    • zgh4693's avatar
      zgh4693
      Occasional Contributor

      output  Tag="A"  about Type='abg'  can u help me  ,thanks in advance!

      nmrao 

       

       

  • nmrao's avatar
    nmrao
    Champion Level 3

    Please see if this is what you need?

     

     

     

    def xml = new XmlParser().parseText(str)
    def result = xml.'**'.findAll{it.@Type == 'abg'}
    result.each { cool ->
    	cool.'**'.findAll {it.@Tag == 'A'}.each {
    		log.info "Type = ${cool.@Type} and Tag = ${it.@Tag}"   	
    	}
    }

     

     

    • zgh4693's avatar
      zgh4693
      Occasional Contributor

      Omg Thanks u so much nmrao , ur script perfect solution my issues ,Thanks u again!!!!

    • zgh4693's avatar
      zgh4693
      Occasional Contributor

      nmrao   how I assert with other values?     how to extract  all @Tag

      expect five ’A‘

       

      for example now i have five  emt =“A”  ,how i assert emt == Tag?,  i dont know how to extract Tag to assert with value

  • nmrao's avatar
    nmrao
    Champion Level 3

    The expected data is unclear.

    There are multiple Cool elements with given type and in each Seletion multiple elements. How do you expect single value as per given xml?

    • zgh4693's avatar
      zgh4693
      Occasional Contributor

      Thanks nmrao ur reply ,sorry i am not describe clearly.I mean output all Tag= “A” ,but need joint   Type=“abg”.  except results should output five Tag='A'. need to filter and output all Tag=“A” under the  Type='abg' .

       

      if can implement,Thanks! Really need ur help 

      nmrao 

  • nmrao's avatar
    nmrao
    Champion Level 3

    Would you mind posting a new question if there are any?

    • zgh4693's avatar
      zgh4693
      Occasional Contributor

      of couse, but i already solved my issues ,Maybe next time i have others issues need u help then i will post new question ,thanks u again!