How do I read the data from the XMLdataBank through groovy and then parse that XML

KesaryKesary Posts: 5

I have a SOAP request which gives XML response. I created one XMLdatabank and mapped the respective node which has children to a variable in the XMLDatabank. In the extension tool I try reading that data of that variable through groovy but it is returning NULL.

I was able to do it with the Json response but not with XML.

import com.parasoft.api.*;

public void sample(Object value, ExtensionToolContext context){

String garbage= context.getValue("Generated Data Source","Response");
Application.showMessage("The data is: " + garbage);

}

Comments

  • benken_parasoftbenken_parasoft Posts: 348 ✭✭✭

    The data bank column "Response" either doesn't exist or wasn't populated. The XML Data Bank has to be configured with an extraction with your desired column name. The XPath used for your extraction must be correct (it will be different for XML than JSON). Additionally, the Data Bank needs to run at least once to populate the data bank column with a value before your script is executed.

  • KesaryKesary Posts: 5

    Yes I ran the test and I was able to see the data in the Databank and once I mapped the node to a variable and clicked on evaluate I was able to see the data too. But in the script I am it is returning Null.

  • benken_parasoftbenken_parasoft Posts: 348 ✭✭✭
    edited January 10

    once I mapped the node to a variable

    In the Data Xank XPath settings dialog under the Data Source Column section, are you using "Custom column name" or "Variable"? context.getValue("Generated Data Source","Response") is what you use for "Custom column name". If instead you are writing the value to a test variable then you would just use context.getValue("Response") assuming your test variable is named "Response".

  • benken_parasoftbenken_parasoft Posts: 348 ✭✭✭

    I am also curious to know why you want to parse the XML from a Groovy script in the first place. There are often better alternatives than scripting.

  • NitziNitzi Posts: 17

    Hi Kesary,

    You can refer below Jython script which works for me:

    from soaptest.api import *
    from com.parasoft.api import *
    from java.util import *
    def sample(input, context):
    garbage= str(context.getValue("Generated Data Source", "Response"))
    Application.showMessage("The data is: " + garbage)
    return garbage

    *Key point is to make sure Data source column name must be 'Response' in the Xml Data Bank

  • benken_parasoftbenken_parasoft Posts: 348 ✭✭✭

    I want to clarify that Groovy works fine too, like your original script. Switching to another scripting language shouldn't solve anything. The issue is one of the things I mentioned in my earlier comments. The data bank column "Response" either doesn't exist or wasn't populated or you are really writing to a test variable in which case your script is calling the wrong method.

  • KesaryKesary Posts: 5

    Hi benken,
    I have found what was causing the issue. As I was running the test separately and extension tool separately I was getting Null response as the data in the "response" variable was wiped off.
    To make things work I must run the thing that creates the generated datasource and that executes the extension tool as a unit and not individually.

    Thank you so much all for the suggestions :smile:

Sign In or Register to comment.