Forum Discussion

Perchishka's avatar
Perchishka
Occasional Contributor
7 years ago
Solved

How to trim white space after XMLParser

Hello!

 

I have an xml.file 

and I need to generate random UUID and Date for every request.

So I decided to write a groovy script:

TimeZone.setDefault(TimeZone.getTimeZone("Europe/Moscow"));
def now = new Date((System.currentTimeMillis() - 3600000L)) 
String s = now.format("yyyy-MM-dd'T'HH:mm:ss.SSS")+"+03:00" 
log.info(s)

context.testCase.setPropertyValue('currentDateForXml', s) 



stringXML = '''<SmsSubscriptionsTarifficationNotifies xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="***.xsd">
   <TarifficationNotify>
    <TransactionId>971f513e-ff56-4b9b-9910-cd1acec12fab</TransactionId>
    <SubscriptionId>f6b3f278-d6fd-48a7-b8c4-5e5570a2b0a0</SubscriptionId>
    <ContentId>de1b0145-f4ae-48b4-a216-f5d8e302760</ContentId>
     <MSISDN>79855575412</MSISDN>
     <AttemptDate>2017-06-16T00:10:13.9+03:00</AttemptDate>
    <Result>false</Result>
     <FaultCode>102</FaultCode>
   </TarifficationNotify> 
   <TarifficationNotify>
.... the same story
   </TarifficationNotify>
 </SmsSubscriptionsTarifficationNotifies>'''


def parsedXml = new XmlParser().parseText(stringXML)
log.info(parsedXml )
def dt = parsedXml.'**'.TransactionId
log.info(dt)
dt.each{it.value =  randomUUID() as String}

def dd  = parsedXml.'**'.AttemptDate
log.info(dd)
dd.each{it.value = s}


def sw = new StringWriter()
new XmlNodePrinter(new PrintWriter(sw)).print(parsedXml)
def newXml = sw.toString()

 context.testCase.setPropertyValue('newXml', newXml) 

and i need to set new xml in property to use it for SOAP request

 

But added to the properties  xml looks like:

<SmsSubscriptionsTarifficationNotifies xmlns="https://moipodpiski.ssl.mts.ru/xsd/SmsSubscriptionsTarifficationNotifies.xsd">  
<TarifficationNotify>         
<TransactionId>       d897fad0-3cd4-4c2a-a317-f69c976906ae     </TransactionId>     
<SubscriptionId>       75cc04d4-b84a-4dbf-a881-d6fe39552083     </SubscriptionId>     
<ContentId>       d2e1edd5-4e3c-4a80-a189-0f52e1b756ec     </ContentId>     
<MSISDN>       79855575412     </MSISDN>     
<AttemptDate>       2017-06-16T00:10:13.9+03:00     </AttemptDate>     
<Result>       false     </Result>     
<FaultCode>       102     </FaultCode>   
</TarifficationNotify> 
</SmsSubscriptionsTarifficationNotifies> 

a lot of white spaces and as a result I have an invalid request

 

Could you help me please

How to remove spaces inside tags?

  • I believe that this question was earlier posted and not visible somehow. Now visible again, it seems.

     

    Any ways, if your object is just to add UUID and date only as shown in the request there, then you do not even require the current Groovy Script step there before Soap Request Test step.

     

    You can directly achieve that within request step itself by the use of in-line script as shown the below request. You can disable groovy step and update the request as below:

     

    <?xml version="1.0" encoding="UTF-8"?>
    <SmsSubscriptionsTarifficationNotifies xmlns="https://moipodpiski.ssl.mts.ru/xsd/SmsSubscriptionsTarifficationNotifies.xsd">
       <TarifficationNotify>
          <TransactionId>${= UUID.randomUUID()}</TransactionId>
          <SubscriptionId>${= UUID.randomUUID()}</SubscriptionId>
          <ContentId>${= UUID.randomUUID()}</ContentId>
          <MSISDN>79855575412</MSISDN>
          <AttemptDate>${= new Date().format("yyyy-MM-dd'T'HH:mm:ss.SSSZ")}</AttemptDate>
          <Result>false</Result>
          <FaultCode>102</FaultCode>
       </TarifficationNotify>
    </SmsSubscriptionsTarifficationNotifies>

    Hope this helps.

4 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3

    I believe that this question was earlier posted and not visible somehow. Now visible again, it seems.

     

    Any ways, if your object is just to add UUID and date only as shown in the request there, then you do not even require the current Groovy Script step there before Soap Request Test step.

     

    You can directly achieve that within request step itself by the use of in-line script as shown the below request. You can disable groovy step and update the request as below:

     

    <?xml version="1.0" encoding="UTF-8"?>
    <SmsSubscriptionsTarifficationNotifies xmlns="https://moipodpiski.ssl.mts.ru/xsd/SmsSubscriptionsTarifficationNotifies.xsd">
       <TarifficationNotify>
          <TransactionId>${= UUID.randomUUID()}</TransactionId>
          <SubscriptionId>${= UUID.randomUUID()}</SubscriptionId>
          <ContentId>${= UUID.randomUUID()}</ContentId>
          <MSISDN>79855575412</MSISDN>
          <AttemptDate>${= new Date().format("yyyy-MM-dd'T'HH:mm:ss.SSSZ")}</AttemptDate>
          <Result>false</Result>
          <FaultCode>102</FaultCode>
       </TarifficationNotify>
    </SmsSubscriptionsTarifficationNotifies>

    Hope this helps.

    • nmrao's avatar
      nmrao
      Champion Level 3
      Perchishka, appreciate if you can mark "Accept as solution" given that it resolved your issue.
      • Perchishka's avatar
        Perchishka
        Occasional Contributor

        Thank you,guys!

        Your advice helped me a lot!