Simplest SOAP example

前端 未结 13 1802
死守一世寂寞
死守一世寂寞 2020-11-22 01:32

What is the simplest SOAP example using Javascript?

To be as useful as possible, the answer should:

  • Be functional (in other words actually work)
  • <
13条回答
  •  野的像风
    2020-11-22 02:15

    The question is 'What is the simplest SOAP example using Javascript?'

    This answer is of an example in the Node.js environment, rather than a browser. (Let's name the script soap-node.js) And we will use the public SOAP web service from Europe PMC as an example to get the reference list of an article.

    const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
    const DOMParser = require('xmldom').DOMParser;
    
    function parseXml(text) {
        let parser = new DOMParser();
        let xmlDoc = parser.parseFromString(text, "text/xml");
        Array.from(xmlDoc.getElementsByTagName("reference")).forEach(function (item) {
            console.log('Title: ', item.childNodes[3].childNodes[0].nodeValue);
        });
    
    }
    
    function soapRequest(url, payload) {
        let xmlhttp = new XMLHttpRequest();
        xmlhttp.open('POST', url, true);
    
        // build SOAP request
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4) {
                if (xmlhttp.status == 200) {
                    parseXml(xmlhttp.responseText);
                }
            }
        }
    
        // Send the POST request
        xmlhttp.setRequestHeader('Content-Type', 'text/xml');
        xmlhttp.send(payload);
    }
    
    soapRequest('https://www.ebi.ac.uk/europepmc/webservices/soap', 
        `
        
        
        
            
                C7886
                CTX
                0
                25
                ukpmc-phase3-wp2b---do-not-reply@europepmc.org
            
        
        `);
    

    Before running the code, you need to install two packages:

    npm install xmlhttprequest
    npm install xmldom
    

    Now you can run the code:

    node soap-node.js
    

    And you'll see the output as below:

    Title:  Perspective: Sustaining the big-data ecosystem.
    Title:  Making proteomics data accessible and reusable: current state of proteomics databases and repositories.
    Title:  ProteomeXchange provides globally coordinated proteomics data submission and dissemination.
    Title:  Toward effective software solutions for big biology.
    Title:  The NIH Big Data to Knowledge (BD2K) initiative.
    Title:  Database resources of the National Center for Biotechnology Information.
    Title:  Europe PMC: a full-text literature database for the life sciences and platform for innovation.
    Title:  Bio-ontologies-fast and furious.
    Title:  BioPortal: ontologies and integrated data resources at the click of a mouse.
    Title:  PubMed related articles: a probabilistic topic-based model for content similarity.
    Title:  High-Impact Articles-Citations, Downloads, and Altmetric Score.
    

提交回复
热议问题