Test for existence of path in XML

Using a javascript script and Spin, how can I test for the existence of an element at an xpath? When I try and do this I get exceptions and can’t seem to be able to prevent the exception. In Java I can use a try/catch block and ignore the exception but this seems to fail in Javascript. Here is one example of what I have tried:

var response = execution.getVariable(‘response’);
try {
response.xPath(“/Message/Payment/TxnList/Txn/txnID”).element().textContent();
} catch(SpinXPathException se) {

}

Hi @mike_g,

Did you try to use a javascript try-catch block like
try { // exception } catch (e) { // handle e }

See this quote from OpenJDK Nashorn/Rhino:

Java exceptions
Rhino wraps Java exceptions as a script object. If you want underlying Java exception, you’ve to use “javaException” property to access it. Nashorn does not wrap Java exceptions. Java exception objects as thrown “as is”. So, in catch blocks you can access Java exceptions “as is”.
Java exceptions
// rhino
try {
java.lang.System.loadLibrary(null)
} catch (e) {
// false!
print(e instanceof java.lang.NullPointerException)
// true
print(e.javaException instanceof java.lang.NullPointerException)
}

// in Nashorn, e instanceof java.lang.NullPointerException is true
// as there is no script wrapping of exceptions.

Cheers,
Christian

Hi Mike,

if you are prepared to use groovy, you could use something like…

def retval = 0
def root = new XmlSlurper().parseText(response)
def txnId = root.Message.Payment.TxnList.Txn.txnID

if (txnId.isEmpty())
    retval = -1
else
    retval = txnId.toInteger();

retval

(or something very similar)

regards

Rob