soap-node-decoder
Version:
A simple package to get the content of a SOAP response node.
80 lines (65 loc) • 2.31 kB
JavaScript
const xml2js = require('xml2js');
const SOAP = {
ENVELOPE: 'soap:Envelope',
BODY: 'soap:Body',
ENVELOPE_ENV: 'SOAP-ENV:Envelope',
BODY_ENV: 'SOAP-ENV:Body'
}
const decodeSoapNode = (node, soap) => {
if(node == undefined){
console.log('Node parameter is undefined or null');
return null;
}
if(soap == undefined){
console.log('Soap parameter is undefined or null');
return null;
}
const parser = new xml2js.Parser();
let nodeValue = null;
let soapEnvelope = SOAP.ENVELOPE;
let soapBody = SOAP.BODY;
parser.parseString(soap, (err, result) => {
if(err){
console.log(err);
return null;
}
/*
* If the soap response is not in a standard format (soap:Envelope, soap:Body)
* the soap envelope and body will be different (SOAP-ENV:Envelope, SOAP-ENV:Body)
* and we need to change the values of the variables
* soapEnvelope and soapBody
*/
if(result['soap:Envelope'] == undefined){
soapEnvelope = SOAP.ENVELOPE_ENV;
soapBody = SOAP.BODY_ENV;
}
/**
* Loop through the soap response and find the node
* that we want to decode and return the value.
* If the node is not found, return null.
*/
for(let key in result[soapEnvelope][soapBody][0]){
//console.log(key);
let keyWithoutBeginning = key.split(':')[1];
if(keyWithoutBeginning == node){
nodeValue = result[soapEnvelope][soapBody][0][key][0];
//console.log(nodeValue);
return;
}
else{
for(let key2 in result[soapEnvelope][soapBody][0][key][0]){
let key2WithoutBeginning = key2.split(':')[1];
if(key2WithoutBeginning == node){
nodeValue = result[soapEnvelope][soapBody][0][key][0][key2][0];
//console.log(nodeValue);
return;
}
}
}
}
console.log(node + ' : No value found');
return null;
});
return nodeValue;
}
module.exports = decodeSoapNode;