Parsing SOAP response in XML by javascript


#1

I’m trying to create simple process with SOAP Service call in Camunda Modeler based on example
soap-service
Because the original service in the internet don’t work now, I write my own simple SOAP Service “Calculator” (in attachment) and try to call it.
SOAP Service is called successfully, but I received an Error message in stage of parsing SOAP Response (please look at screenshots).
Original SOAP Service response:

HTTP/1.1 200 OK
Date: Mon, 01 Oct 2018 12:11:20 GMT
Transfer-encoding: chunked
Content-type: text/xml; charset=utf-8

<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:addResponse xmlns:ns2="http://myws/">
<return>7</return>
</ns2:addResponse>
</S:Body>
</S:Envelope>

My Output parameters in “Connector” tab where I try to get “return” value = 7:
Name:
return

Type:
Text

Value:
${S(response)
.childElement(“Body”)
.childElement(“http://myws/”, “addResponse”)
.childElement(“return”)
.textContent()}

Error Message in Camunda TaskList:
The process could not be started. : Cannot instantiate process definition MySuperCalculator:1:f5697362-c559-11e8-a93c-1831bf42b64c: Error while evaluating expression: ${S(response) .childElement("Body") .childElement("http://myws/", "addResponse") .childElement("return") .textContent()}. Cause: org.camunda.spin.xml.SpinXmlElementException: SPIN/DOM-XML-01007 Unable to find child element with namespace 'http://myws/' and name 'return'

Could you help me to understand what’s wrong ?
P.S. If you want to repeat my example, please, don’t forget to change URL to your own in Connector tab.
invokeCalculator2.bpmn (5.0 KB)


#3

My found solution of parsing XML Array in javascript is in attachement.
Parse_XML_v03.bpmn (9.0 KB)

SOAP response:

javascript code:

    print('parsedStr =' + parsedStr);

    var arg0=S(parsedStr).xPath('string(/answer/arguments/arg0)').string();
    print('arg0=' + arg0);
    var arg1=S(parsedStr).xPath('string(/answer/arguments/arg1)').string();
    print('arg1=' + arg1);

    var method1=S(parsedStr).xPath('string(/answer/results/result[1]/method)').string();
    print('method1 = ' + method1);

    var result_id_33 = S(parsedStr).xPath("string(/answer/results/result[3]/@id)").string();
    print("result_id_33 = " + result_id_33 );

    var count=S(parsedStr).childElement("results").childElements("result").size();
    print('count=' + count);

    print("--------------------------===--------------------------------" );

    var i;
    for (i=0; i<count; i++)
    {
      var n=i+1;
      var param1="string(/answer/results/result[" +n+ "]/@id)";
      var param2="string(/answer/results/result[" +n+ "]/method)";
      var param3="string(/answer/results/result[" +n+ "]/amount)";

      print("n=" + n + " result id=" + S(parsedStr).xPath(param1).string() + "  method=" + S(parsedStr).xPath(param2).string() + "  amount=" + S(parsedStr).xPath(param3).string() );
    }
    var ResultCode = S(parsedStr).xPath("string(/answer/ServiceResult/ResultCode)").string();
    print("ResultCode = " + ResultCode );
    var ResultText = S(parsedStr).xPath("string(/answer/ServiceResult/ResultText)").string();
    print("ResultText= " + ResultText);

Output was:
------------------------- START FLOW --------------------------------------
------- Task_Print -------
===== param1=22
===== new param1=45
====-=-=-=-===== new param1=55
SOAPHeaders={Content-type=text/xml; charset=utf-8, Date=Thu, 18 Oct 2018 08:36:17 GMT, Transfer-encoding=chunked}
SOAPStatusCode=200
SOAPResponse=<?xml version="1.0" encoding="UTF-8"?><S:Envelope xmlns:S=“http://schemas.xmlsoap.org/soap/envelope/”>
<S:Body>
<ns2:fixedlistResponse xmlns:ns2=“http://myws/”>
<answer><arguments><arg0>20</arg0><arg1>4</arg1></arguments><results><result id=“1”><method>add</method><amount>24.0</amount></result><result id=“2”><method>div</method><amount>5.0</amount></result><result id=“33”><method>mult</method><amount>80.0</amount></result><result id=“44”><method>minus</method><amount>16.0</amount></result></results><ServiceResult><ResultCode>0</ResultCode><ResultText>Ok</ResultText></ServiceResult></answer>
</ns2:fixedlistResponse>
</S:Body>
</S:Envelope>

parsedStr =<answer><arguments><arg0>20</arg0><arg1>4</arg1></arguments><results><result id="1"><method>add</method><amount>24.0</amount></result><result id="2"><method>div</method><amount>5.0</amount></result><result id="33"><method>mult</method><amount>80.0</amount></result><result id="44"><method>minus</method><amount>16.0</amount></result></results><ServiceResult><ResultCode>0</ResultCode><ResultText>Ok</ResultText></ServiceResult></answer>
arg0=20
arg1=4
method1 = add
result_id_33 = 33
count=4
--------------------------===--------------------------------
n=1 result id=1  method=add  amount=24.0
n=2 result id=2  method=div  amount=5.0
n=3 result id=33  method=mult  amount=80.0
n=4 result id=44  method=minus  amount=16.0
ResultCode = 0
ResultText= Ok