简体   繁体   English

如何使用 node.js 获取 SOAP 主体?

[英]How to get SOAP body with node.js?

I have incoming SOAP message in a format like this:我有以下格式的传入 SOAP 消息:

const soap = `
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
    <env:Header/>
    <env:Body property="def">
        <Root>
            <Abc attr="abc">123456</Abc>
        </Root>
    </env:Body>
</env:Envelope>
`;

What I expect to get is the content of <env:Body> unchanged:我期望得到的是<env:Body>不变:

<Root>
    <Abc attr="abc">123456</Abc>
</Root>

To extract the body I use simple regex:要提取正文,我使用简单的正则表达式:

function getSoapBody(xmlStr) {
    let soapBody = null;
    if (xmlStr) {
        const soapBodyRegex = /<env:Body>([\s\S]*)<\/env:Body>/im;
        const soapBodyRegexMatchResult = xmlStr.match(soapBodyRegex);
        soapBody = soapBodyRegexMatchResult[1];
    }
    return soapBody;
}

However, getting body with regex is not ideal of course.但是,使用正则表达式获取 body 当然并不理想。

Moreover, I don't want to reinvent the wheel and I'm looking for a solution (or even npm package) that will basically getSoapBody regardless of prefix used (the regex above will fail if the prefix changes) or if there are optional attributes etc.此外,我不想重新发明轮子,我正在寻找一种解决方案(甚至 npm 包),无论使用什么前缀(如果前缀发生变化,上面的正则表达式都会失败)或者是否有可选属性,它基本上都会 getSoapBody等。

You can use xml2js to parse:您可以使用 xml2js 来解析:

var xml2js = require('xml2js');
var builder = new xml2js.Builder();

const soap = `
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
    <env:Header/>
    <env:Body optional="abc">
        <Root>
            <Abc attr="1">123456</Abc>
        </Root>
    </env:Body>
</env:Envelope>
`;

var options = {explicitArray: false, tagNameProcessors: [xml2js.processors.stripPrefix] };

xml2js.parseString(soap, options, (err, result) => {
    if (err) {
        console.log('An error has occurred: ' + err);
        return;
    } 

    // Get the soap body object
    var soapBody = result.Envelope.Body;

    // Remove optional attribute(s) from <Body> element.
    if (soapBody.$) {
        delete soapBody.$;
    }

    // Get the body XML if needed
    var soapBodyXML = builder.buildObject(soapBody);

    console.log(soapBodyXML);
});

I used the fast-xml-parser ( https://www.npmjs.com/package/fast-xml-parser/v/2.9.0 ) to achieve this by using the below options to exclude namespaces & prefixes and to ensure that attributes are under a node 'attr':我使用 fast-xml-parser ( https://www.npmjs.com/package/fast-xml-parser/v/2.9.0 ) 通过使用以下选项来排除命名空间和前缀并确保属性位于节点“attr”下:

async function stripBodyFromSoapEnvelope(inputBody){
    const xmlParser = require('fast-xml-parser');        
    let options = {
            attributeNamePrefix : '',
            attrNodeName: 'attr',
            ignoreNameSpace: true,
            ignoreAttributes: false
        };
    const soapenv = xmlParser.parse(inputBody,options);
    return(soapenv.Envelope.Body);
}

Hope this helps.希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM