Forum Discussion

gregory_kozel's avatar
gregory_kozel
New Contributor
15 years ago

Dynamic XML generation in Request

Greetings,

I was wondering if SoapUI has the capability to generate XML tags in the request message.

example of what I am asking - take this generic soap request:


 

 
   
     
        1
        2
        3
     

   

 


In this example, fields 1 2 and 3 are being parsed out from a .txt file:
field1=1
field2=2
field3=3

and my Properties step is being populated accordingly.  However, I would like to be able to parse out a .txt file that might have these fields repeating, i.e. simulating multiple rows from a database

so the .txt file would look somthing like follows:
field1=1
field2=2
field3=3
field1=4
field2=5
field3=6

My script will overwrite the Properties in my testCase with 4, 5 & 6 respectively (although this is a different issue).

I'd like to find a way to dynamically generate 3 new xml tags in this case for the request message, to look like the following:


 

 
   
     
        1
        2
        3
     

     
        4
        5
        6
     

   

 


Thank you in advance for any advice you may provide.  If my question is unclear, I will gladly provide more detail where possible.

~Greg

5 Replies

  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi Gregory,


    I understand that you're trying to generate sets of parameters based on data loaded from a properties file, containing name=value pairs. I recommend reading Template Driven Testing and Data Driven Testing sections of User Guide. Depending on your preferred format you can define variables in different ways that expand properties into desired XML nodes within a SOAP message template.

    Your requirement can be supported with effortlessly using DataSource feature that is available in soapUI pro.

    I hope this my suggestions have helped you.


    Cheers!
    /Nenad Nikolic a.k.a. Shonzilla
  • Hi Shonzilla, Thank you for the response.

    As I do not have Pro at the moment, I am trying to think up other ways to generate the XML.  At this point I feel as though it can be done using Groovy to read in a flatfile of name=value pairs, parse out the name=value pairs, and build the XML via importing groovy.xml.MarkupBuilder.

    Since I am shifting gears more on the XML builder aspect of things, here is the code I'm starting with:


    import groovy.xml.MarkupBuilder
    def writer = new StringWriter()
    def xml = new MarkupBuilder( writer )

    def list = []
    new File("filename.txt").eachline { line -> list.add( line ) }

    context.setProperty( "values", list )
    context.setProperty( "index", 0 )
    def values = context.getProperty( "values" )
    def index = context.getProperty( "index" )

    while( index < values.size()  ) {
      def str = values[index]
      def parseValue = str.indexOf( "=" )
      def nextValueName =  str.substring( 0, parseValue )
      def nextValue = str.substring( parseValue+1 )

      xml."$nextValueName"( nextValue )

      index++
    }

    outPath = "filename.xml"
    outFile = new File( outPath )
    outFile << writer



    With this code, filename.xml is simply a list of XML tags with the respective values in each tag.  So if we take my example above, a flatfile with name=value pairs of:
    field1=1
    field2=2
    field3=3
    field1=4
    field2=5
    field3=6

    would generate the following:

      1
      2
      3
      4
      5
      6


    I would need some way to get the following xml output:

      1
      2
      3


      4
      5
      6


    I am new to this (only started playing with this last week).  Perhaps a better question would be, is there a comprehensive "using Groovy to build XML" guide out there I can start reading?

    Again, Thank you very much for your patience and assistence.

    ~Greg
  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi Gregory,


    You're on the right path...

    The next problem you're trying to solve is not specific to creating XML from Groovy nor Groovy language itself. Rather, it's a more general programming problem: how do I detect groups of parameter name-value pairs that are contained within a greater set of name-value pairs with repeated parameter names?.

    As an easy way to carry on I would suggest using a syntax like:

    message1.field1=1
    message1.field2=2
    message1.field3=3
    message2.field1=4
    message2.field2=5
    message2.field3=6

    and then parsing the parameter name to extract the message number, detect when you need to output a complete element (using groovy.xml.MarkupBuilder#createNode)  and start parsing new message's fields. Clearly, in this approach you would need to keep single message's fields one next to another but since the input file is under your control, that won't pose a problem. There are many ways to parse text in Java/Groovy so that should be a lesser problem for you now.

    Good luck!


    Cheers!
    /Nenad Nikolic a.k.a. Shonzilla
  • wyderp's avatar
    wyderp
    New Contributor
    so once the code is in place to parse the different messages.N, how would you loop through a template with each new set of values?