Ask a Question

How to trim white space after XMLParser

SOLVED
Perchishka
Occasional Contributor

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?

1 ACCEPTED SOLUTION

Accepted Solutions
nmrao
Community Hero

Re: How to trim white space after XMLParser

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.



Regards,
Rao.

View solution in original post

4 REPLIES 4
PaulMS
Super Contributor

Re: How to trim white space after XMLParser

 

See this answer https://community.smartbear.com/t5/SoapUI-NG/Groovy-Script-XmlNodePrinter-adds-one-extra-line-while-...

 

For example

 

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

 

 

nmrao
Community Hero

Re: How to trim white space after XMLParser

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.



Regards,
Rao.

View solution in original post

nmrao
Community Hero

Re: How to trim white space after XMLParser

@Perchishka, appreciate if you can mark "Accept as solution" given that it resolved your issue.


Regards,
Rao.
Perchishka
Occasional Contributor

Re: How to trim white space after XMLParser

Thank you,guys!

Your advice helped me a lot!

cancel
Showing results for 
Search instead for 
Did you mean: