Forum Discussion

asn_007's avatar
asn_007
Contributor
8 years ago

Create Dynamic XML using Groovy & parameterize the arguments

I would like to create a dynamic xml where some of the nodes and their sub nodes repeat based on the number of times I want them to and then the values of each node elements need to be parameterized from an external file.

 

an example of what I am trying to do is as below :

In the xml below I have some common elements(ex : <day> which does not repeat in the xml and I can have a single property for that and send values from an external file using a simple groovy.

The <food> tag set needs to repeat based on my preference(If I want it to be 10 then it should create 10 such nodes.

I dont mind creating multiple columns in a csv file for each value of the food tag set & reading using groovy.

 

<?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu>
<day>${Properties#day}</day> //Will parameterize this in Properies tab and call from Groovy
<time>08:00:00</time> //Remains static
<food>
<name>${Properties#name1}</name> //Needs Parameterization
<price>${Properties#price1}</price> //Needs Parameterization
<description>
Two of our famous Belgian Waffles with plenty of real maple syrup
</description> //Needs Parameterization
<calories>650</calories> //Needs Parameterization
</food>
<food>
<name>${Properties#name2}</name>
<price>${Properties#price2}</price>
<description>
Light Belgian waffles covered with strawberries and whipped cream
</description>
<calories>900</calories>
</food>
<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>
Belgian waffles covered with assorted fresh berries and whipped cream
</description>
<calories>900</calories>
</food>
</breakfast_menu>

 

Can someone please guide me on how this xml generation & data parameterization be done

 

Thanks

A

  • Here is how you can do it using Groovy.

     

    Since you have more data, you can put it in a text file as shown below:

     

     

    name1, price1, description1, calories1
    name2, price2, description2, calories2

     

     

    And the Script goes here:

     

     

    //Make sure you pass the data.txt with absolute path in unix style
    def lines = new File('data.txt').readLines()
    def builder = new StreamingMarkupBuilder()
    builder.encoding = 'UTF-8'
    def xml = builder.bind {
        mkp.xmlDeclaration()
    //Use below line if you need name space as needed, remove otherwise
        namespaces << [ns:'http://example/break/fast']
        breakfast_menu {
            day (new Date().format('EEE MMM d yyyy'))
            time( '08:00:00' )
            lines.each { line ->
                def data = line.split(',')*.trim()
                food {
                    name (data[0])
                    price (data[1])
                    description (data[2])
                    calories (data[3])
                }
            }
        }
    }
    
    def updatedXml = groovy.xml.XmlUtil.serialize(xml)
    log.info updatedXml

     

     

    Output

     

    <?xml version="1.0" encoding="UTF-8"?>
    <breakfast_menu xmlns:ns="http://example/break/fast">
      <day>Thu Apr 27 2017</day>
      <time>08:00:00</time>
      <food>
        <name>name1</name>
        <price>price1</price>
        <description>description1</description>
        <calories>calories1</calories>
      </food>
      <food>
        <name>name2</name>
        <price>price2</price>
        <description>description2</description>
        <calories>calories2</calories>
      </food>
    </breakfast_menu>
  • nmrao's avatar
    nmrao
    8 years ago

    asn_007,

    Ignore the first row. Haven't tried, Can you give a try ?

            lines.eachWithIndex { line, index ->
                if (index) {
                	def data = line.split(',')*.trim()
                	food {
                	    name (data[0])
                	    price (data[1])
                	    description (data[2])
                	    calories (data[3])
                	}
                }	    	
            }
  • nmrao's avatar
    nmrao
    Champion Level 3

    Here is how you can do it using Groovy.

     

    Since you have more data, you can put it in a text file as shown below:

     

     

    name1, price1, description1, calories1
    name2, price2, description2, calories2

     

     

    And the Script goes here:

     

     

    //Make sure you pass the data.txt with absolute path in unix style
    def lines = new File('data.txt').readLines()
    def builder = new StreamingMarkupBuilder()
    builder.encoding = 'UTF-8'
    def xml = builder.bind {
        mkp.xmlDeclaration()
    //Use below line if you need name space as needed, remove otherwise
        namespaces << [ns:'http://example/break/fast']
        breakfast_menu {
            day (new Date().format('EEE MMM d yyyy'))
            time( '08:00:00' )
            lines.each { line ->
                def data = line.split(',')*.trim()
                food {
                    name (data[0])
                    price (data[1])
                    description (data[2])
                    calories (data[3])
                }
            }
        }
    }
    
    def updatedXml = groovy.xml.XmlUtil.serialize(xml)
    log.info updatedXml

     

     

    Output

     

    <?xml version="1.0" encoding="UTF-8"?>
    <breakfast_menu xmlns:ns="http://example/break/fast">
      <day>Thu Apr 27 2017</day>
      <time>08:00:00</time>
      <food>
        <name>name1</name>
        <price>price1</price>
        <description>description1</description>
        <calories>calories1</calories>
      </food>
      <food>
        <name>name2</name>
        <price>price2</price>
        <description>description2</description>
        <calories>calories2</calories>
      </food>
    </breakfast_menu>
    • asn_007's avatar
      asn_007
      Contributor

      Thank you Rao. This has solved one of my one problems. 

      nmrao Using the same code, how do I ignore the first line in the "data" text file. if that was a header.

      • asn_007's avatar
        asn_007
        Contributor

        nmraoUsing the same code, how do I ignore the first line in the "data" text file. if that was a header.

    • SuperSingh's avatar
      SuperSingh
      Contributor

      Hello nmrao,

       

      With regards to this thread, you explained to take input using text file. Can you please share the steps to use excel or csv instead. I have lot of nodes in my request body for which excel will be easy to maintain.

       

      Thanks in advance !

      • nmrao's avatar
        nmrao
        Champion Level 3
        Sorry for the delay. Hope you might have figured it out by now.
  • MRAO's avatar
    MRAO
    New Contributor

    HI below is my request Xml i want to dynamically increase the Participant tag when the count in request data sheet increases, Can somebody please help with the solution Thank you

     

     

     

    <SightSeeingInterfaceRQ xmlns="http://www.coxandkings.com/scota">
       <RequestHeader>
          <UserID>${#TestSuite#UserID}</UserID>
          <SessionID>${#TestSuite#SessionID}</SessionID>
          <TransactionID>${#TestSuite#TransactionID}</TransactionID>
          <SupplierCredentialsList>
             <SupplierCredentials>
                <SupplierID>${#TestSuite#SupplierID}</SupplierID>
                <Sequence>1</Sequence>
                <Credentials>
                   <!--1 or more repetitions-->
                   <Credential name="${#TestSuite#CredOne}" isEncrypted="${#TestSuite#Encyp}">${#TestSuite#USERNAME}</Credential>
                   <Credential name="${#TestSuite#CredTwo}" isEncrypted="${#TestSuite#Encyp}">${#TestSuite#PASSWORD}</Credential>
                   <Credential name="${#TestSuite#CredThree}" isEncrypted="${#TestSuite#Encyp}">${#TestSuite#Add}</Credential>
                    <OperationURLs>
                       <!--1 or more repetitions-->
                      <OperationURL operation="all">${#TestSuite#OperationURL}</OperationURL>
                      <OperationURL operation="search">${#TestSuite#OperationSearchURL}</OperationURL>
                      <OperationURL operation="getdetails">${#TestSuite#OperationGetDetailsURL}</OperationURL>
                     <!-- <OperationURL operation="all">${#TestSuite#OperationGetPoliciesURL}</OperationURL>-->
                      <OperationURL operation="reprice">${#TestSuite#OperationRepriceURL}</OperationURL>
                      <OperationURL operation="book">${#TestSuite#OperationBookURL}</OperationURL>
                      <OperationURL operation="retrieve_booking">${#TestSuite#OperationRetrieveURL}</OperationURL>
                      <OperationURL operation="cancel">${#TestSuite#OperationCancel}</OperationURL>
                   </OperationURLs>
                </Credentials>
             </SupplierCredentials>
          </SupplierCredentialsList>
       </RequestHeader>
       <RequestBody>
         <OTA_TourActivityAvailRQ>
          <ProcessingInformation PricingCurrency="${#TestSuite#PricingCurrency}"/>
                <TourActivity>
                 <Location>
                      <Address>
                         <CountryName Code="${#TestSuite#CountryName}"/>
                      </Address>
                      <Region RegionCode="${#TestSuite#RegionCode}" />
                   </Location>
                   <Schedule StartPeriod="${#TestSuite#StartPeriod}" EndPeriod="${#TestSuite#EndPeriod}"/>
                 
                 <!-- Repeat participant count for each individual with quantity hardcoded as 1-->
                   <ParticipantCount Quantity="${#TestSuite#Quantity}" Age="${#TestSuite#Age}">
                      <QualifierInfo Extension="">${#TestSuite#Extension}</QualifierInfo>
                      <!-- Value Can be Adult,Child,Infant -->
                   </ParticipantCount>
                    <ParticipantCount Quantity="${#TestSuite#Quantity2}" Age="${#TestSuite#Age2}">
                      <QualifierInfo Extension="">${#TestSuite#Extension2}</QualifierInfo>
                      <!-- Value Can be Adult,Child,Infant -->
                   </ParticipantCount>
                  
                    
                  
                </TourActivity>
             </OTA_TourActivityAvailRQ>
       </RequestBody>
    </SightSeeingInterfaceRQ>