Parse XML Values

Applies to ReadyAPI 3.56, last modified on November 21, 2024

You can work with XML in your scripts. ReadyAPI provides custom methods used to get XML nodes and work with them. For information about advanced parsing of XML, see XML Parsing in ReadyAPI Test.

Where to use

There are several ways to run these scripts:

Examples

Getting node values

To get XML node values, you use the groovyUtils and XmlHolder objects. Here is how you can get values from the Test Request – login request in the SOAP Sample project:

Sample XML response

Groovy

// Create the groovyUtils and XmlHolder objects to hold the response XML
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder( 'Test Request - login#Response' )

// Loop item nodes in the response message
for( item in holder.getNodeValues( "//faultcode" ))
    log.info "Item : [$item]"
    
// If the desired content is namespace qualified, you need to define the namespace first
// Define the namespace
holder.namespaces['ns'] = 'http://www.soapui.org/sample/'

// Loop item nodes in the response message
for( item in holder.getNodeValues( "//ns:loginFault" ))
    log.info "Item : [$item]"

Counting Nodes

When you already have an XML, you can use normal operations to work with it. For example, to count the number of item elements in the XML, use the following code:

Sample XML response

Groovy

// Create the groovyUtils and XmlHolder objects to hold the response XML
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder( "Test Request - search#Response" )

// Count the number of item elements in the XML
def numberOfItemsInResponse = holder["count(//Body/searchResponse/searchResponseContent/item)"]
log.info numberOfItemsInResponse

See Also

Groovy Scripting Samples
Testing APIs

Highlight search results