Nicolas de Fontenay
Nicolas de Fontenay

Reputation: 2200

how to parse the following soap response with javascript? (Can't use jquery)

give the following SOAP response:

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <ns3:signOnResponse xmlns:ns3="http://www.verimatrix.com/omi">
         <sessionHandle>
            <ns1:handle xmlns:ns1="http://www.verimatrix.com/schemas/OMItypes.xsd">h0PtwMXVmHp6Oqy7A6CmcrFrnVM=</ns1:handle>
         </sessionHandle>
         <result>
            <ns1:resultId xmlns:ns1="http://www.verimatrix.com/schemas/OMItypes.xsd">admin</ns1:resultId>
            <ns1:resultCode xmlns:ns1="http://www.verimatrix.com/schemas/OMItypes.xsd">0</ns1:resultCode>
            <ns1:resultText xmlns:ns1="http://www.verimatrix.com/schemas/OMItypes.xsd">Success</ns1:resultText>
         </result>
      </ns3:signOnResponse>
   </soapenv:Body>
</soapenv:Envelope>

How can I obtain the handle :h0PtwMXVmHp6Oqy7A6CmcrFrnVM= located at ns1:handle?

My code is the following:

responseXml = responseXml.replace(/^<\?xml\s+version\s*=\s*(["'])[^\1]+\1[^?]*\?>/, "");
var response = new XML(responseXml);

// Determine the namespace of the SOAP Envelope:
//
var soap = response.namespace();

// Specify the namespace of the verify email response:
//
var ws = response.*.*.namespace();

// Set this namespace as a default to make parsing the response easier:

default xml namespace = ws

var responseBody = response.soap::Body.signOnResponse;
var handle = responseBody.signOnResponse.sessionHandle.handle

Alert(handle);

The problem is that it returns a null value...

When I do Alert(responseBody.toXMLString());

I get:

<ns3:signOnResponse xmlns:ns3="http://www.verimatrix.com/omi" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <sessionHandle>
    <ns1:handle xmlns:ns1="http://www.verimatrix.com/schemas/OMItypes.xsd">TBdn27dfFPlpWG/HTRgH16LsrkI=</ns1:handle>
  </sessionHandle>
  <result>
    <ns1:resultId xmlns:ns1="http://www.verimatrix.com/schemas/OMItypes.xsd">admin</ns1:resultId>
    <ns1:resultCode xmlns:ns1="http://www.verimatrix.com/schemas/OMItypes.xsd">0</ns1:resultCode>
    <ns1:resultText xmlns:ns1="http://www.verimatrix.com/schemas/OMItypes.xsd">Success</ns1:resultText>
  </result>
</ns3:signOnResponse>

I'm using Pentaho Data Integration and parsing the response with javascript step.

Thank you

Upvotes: 2

Views: 4797

Answers (1)

marpontes
marpontes

Reputation: 178

if you're only interested in this specific piece of your response, then let's just treat the response itself as a string and grab the interesting part in it.

The following code would do it for you:

var ns1Handle = responseXml.substring(responseXml.indexOf("<ns1:handle"));
ns1Handle = ns1Handle.substring(ns1Handle.indexOf(">")+1)
ns1Handle = ns1Handle.substring(0,ns1Handle.indexOf("<"))

Check the working script at http://jsfiddle.net/Qz2ZN/ - and look at your javascript console.

:)

Upvotes: 1

Related Questions