Getting and setting variable values
Related Topics ...
We can access all of the variables in the Web application by calling the webAppAccess.getVariables() method. Typically, you get or set the value of one variable at a time.
To get the value of a variable of a simple type (String, int, etc.):
String valueOfMyVariable = webAppAccess.getVariables().getString("MyVariable");
To get the XML structure stored in a variable of the XMLData type:
IXml valueOfMyXMLVariable = webAppAccess.getVariables().getXml("MyXMLVariable");
To get the text value of an element in a variable of the XMLData type:
We can get the text value for a particular element in the XML structure contained in a variable by calling the getXmlText() method and supplying the variable name and the XPath to the element whose value you want to return. The following code sample returns the text for the <name /> element of the second <customer /> element in the <customers /> structure:
webAppAccess.getVariables().getXmlText("VariableTest", "customers/customer[1]/name");
To get an element in a variable of the XMLData type:
We can get a particular element in the XML structure contained in a variable by calling the getXmlElement() method and supplying the variable name and the XPath to the element you want to return. The following code sample returns the second <customer /> element in the <customers /> structure:
webAppAccess.getVariables().getXmlElement("VariableTest", "customers/customer[1]");
To set the value of a variable of a simple type:
webAppAccess.getVariables().setInt("MyIntVariable", 4);
To modify the XML structure stored in a variable of the XMLData type:
We can alter the XML structure stored in a variable by getting the XML structure with the getXML() method and then using the IXml API to modify the structure. The getXML() method returns a reference to the structure, so once you make the modifications to the structure returned by getXML(), there is no need to "set" the structure back into the variable.
The following code sample removes the second <customer /> element from the <customers /> structure:
IXml customers = webAppAccess.getVariables().getXml("CustomerList");
customers.removeChildElement(customers.findElement("customers/customer[1]"));
To set the text value of an element in a variable of the XMLData type:
IXml customers = webAppAccess.getVariables().getXml("CustomerList");
customers.setText("customers/customer[2]/name", "NewName");