Created
May 31, 2019 06:45
-
-
Save liudonghua123/3d96acab29ce8d627feeb9c8858a38ea to your computer and use it in GitHub Desktop.
A simple node app for soap operation demonstration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getCircularReplacer = () => { | |
const seen = new WeakSet(); | |
return (key, value) => { | |
if (typeof value === "object" && value !== null) { | |
if (seen.has(value)) { | |
return; | |
} | |
seen.add(value); | |
} | |
return value; | |
}; | |
}; | |
const fs = require("fs"); | |
const soap = require('soap'); | |
const xml2js = require('xml2js'); | |
const parser = new xml2js.Parser(); | |
(async () => { | |
const url = 'https://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl'; | |
let args = {LatLonListZipCode: 75001}; | |
const client = await soap.createClientAsync(url); | |
//client.describe(); | |
//console.info(`client: ${JSON.stringify(client, getCircularReplacer(), 2)}`); | |
// fs.writeFile("temp.txt", JSON.stringify(client, getCircularReplacer(), 2), (err) => { | |
// if (err) console.log(err); | |
// console.log("Successfully Written to File."); | |
// }); | |
const [result] = await client.LatLonListZipCodeAsync(args); | |
console.log(`result: ${JSON.stringify(result, getCircularReplacer(), 2)}`); | |
parser.parseString(result.listLatLonOut.$value, (err, result) => { | |
console.dir(err); | |
console.dir(result.dwml.latLonList); | |
}) | |
})() |
Author
liudonghua123
commented
May 31, 2019
Another way for making soap request using strong-soap
const fs = require('fs');
const util = require('util');
const soap = require('strong-soap').soap;
const XMLHandler = soap.XMLHandler;
const xmlHandler = new XMLHandler();
const url = 'https://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl';
const options = {};
soap.createClient(url, options, async (err, client) => {
let args = { LatLonListZipCode: 75001 };
try {
const { result, envelope } = await client.LatLonListZipCode(args);
//response envelope
console.log(`Response Envelope: \n${envelope}`);
//'result' is the response body
console.log(`Result: \n${JSON.stringify(result)}`);
const node = xmlHandler.jsonToXml(
null,
null,
XMLHandler.createSOAPEnvelopeDescriptor('soap'),
result
);
const xml = node.end({ pretty: true });
console.log(`xml:\n${xml}`);
const root = xmlHandler.xmlToJson(null, xml, null);
console.log(`root:\n${util.inspect(root, { depth: null })}`);
} catch (err) {
// handle error
console.error(err);
}
});
But the result also have to transform, the XMLHandler
seems not enough.
The result of the following code is:
Response Envelope:
<?xml version="1.0" encoding="ISO-8859-1"?><SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:LatLonListZipCodeResponse xmlns:ns1="https://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl"><listLatLonOut xsi:type="xsd:string"><?xml version='1.0'?><dwml version='1.0' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='https://graphical.weather.gov/xml/DWMLgen/schema/DWML.xsd'><latLonList>32.9612,-96.8372</latLonList></dwml></listLatLonOut></ns1:LatLonListZipCodeResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>
Result:
{"listLatLonOut":{"$attributes":{"$xsiType":{"type":"string","xmlns":"http://www.w3.org/2001/XMLSchema"}},"$value":"<?xml version='1.0'?><dwml version='1.0' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='https://graphical.weather.gov/xml/DWMLgen/schema/DWML.xsd'><latLonList>32.9612,-96.8372</latLonList></dwml>"}}
xml: <listLatLonOut xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns1="http://www.w3.org/2001/XMLSchema" xsi:type="ns1:string"><?xml version='1.0'?><dwml version='1.0' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='https://graphical.weather.gov/xml/DWMLgen/schema/DWML.xsd'><latLonList>32.9612,-96.8372</latLonList></dwml></listLatLonOut>
root: {
listLatLonOut: {
'$attributes': {
'$xsiType': { type: 'string', xmlns: 'http://www.w3.org/2001/XMLSchema' }
},
'$value': "<?xml version='1.0'?><dwml version='1.0' " +
"xmlns:xsd='http://www.w3.org/2001/XMLSchema' " +
"xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " +
"xsi:noNamespaceSchemaLocation='https://graphical.weather.gov/xml/DWMLgen/schema/DWML.xsd'><latLonList>32.9612,-96.8372</latLonList></dwml>"
}
}
C:\Users\Liu.D.H\Desktop\web>node strong-soap-test.js
Response Envelope:
<?xml version="1.0" encoding="ISO-8859-1"?><SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:LatLonListZipCodeResponse xmlns:ns1="https://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl"><listLatLonOut xsi:type="xsd:string"><?xml version='1.0'?><dwml version='1.0' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='https://graphical.weather.gov/xml/DWMLgen/schema/DWML.xsd'><latLonList>32.9612,-96.8372</latLonList></dwml></listLatLonOut></ns1:LatLonListZipCodeResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>
Result:
{"listLatLonOut":{"$attributes":{"$xsiType":{"type":"string","xmlns":"http://www.w3.org/2001/XMLSchema"}},"$value":"<?xml version='1.0'?><dwml version='1.0' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='https://graphical.weather.gov/xml/DWMLgen/schema/DWML.xsd'><latLonList>32.9612,-96.8372</latLonList></dwml>"}}
xml:
<listLatLonOut xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns1="http://www.w3.org/2001/XMLSchema" xsi:type="ns1:string"><?xml version='1.0'?><dwml version='1.0' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='https://graphical.weather.gov/xml/DWMLgen/schema/DWML.xsd'><latLonList>32.9612,-96.8372</latLonList></dwml></listLatLonOut>
root:
{
listLatLonOut: {
'$attributes': {
'$xsiType': { type: 'string', xmlns: 'http://www.w3.org/2001/XMLSchema' }
},
'$value': "<?xml version='1.0'?><dwml version='1.0' " +
"xmlns:xsd='http://www.w3.org/2001/XMLSchema' " +
"xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " +
"xsi:noNamespaceSchemaLocation='https://graphical.weather.gov/xml/DWMLgen/schema/DWML.xsd'><latLonList>32.9612,-96.8372</latLonList></dwml>"
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment