如何更改node.js中soap包生成的xml?

Posted

技术标签:

【中文标题】如何更改node.js中soap包生成的xml?【英文标题】:How to change xml generated by soap package in node.js? 【发布时间】:2019-10-27 17:22:24 【问题描述】:

我正在使用SOAP 包通过xml 向workday-api 发出请求。

问题在于soap包生成的xml。

生成的 XML

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:wd-wsdl="urn:com.workday/bsvc/Recruiting"
    xmlns:wd="urn:com.workday/bsvc"
    xmlns:nyw="urn:com.netyourwork/aod">
    <soapenv:Header>
        <wsse:Security soapenv:mustUnderstand="1"
            xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
            xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
            <wsse:UsernameToken>
                <wsse:Username>Username</wsse:Username>
                <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">Password</wsse:Password>
            </wsse:UsernameToken>
        </wsse:Security>
    </soapenv:Header>
    <soapenv:Body>
        <wd:Get_Applicants_Request
            xmlns:wd="urn:com.workday/bsvc"
            xmlns="urn:com.workday/bsvc">
            <bsvc:Get_Applicants_Request bsvc:version="v32.1">
                <bsvc:Request_Criteria>
                    <bsvc:Email_Address>abc@gmail.com</bsvc:Email_Address>
                </bsvc:Request_Criteria>
                <bsvc:Response_Group>
                    <bsvc:Include_Reference>true</bsvc:Include_Reference>
                    <bsvc:Include_Personal_Information>true</bsvc:Include_Personal_Information>
                    <bsvc:Include_Recruiting_Information>true</bsvc:Include_Recruiting_Information>
                    <bsvc:Include_Qualification_Profile>true</bsvc:Include_Qualification_Profile>
                    <bsvc:Include_Resume>false</bsvc:Include_Resume>
                    <bsvc:Include_Background_Check>false</bsvc:Include_Background_Check>
                    <bsvc:Include_External_Integration_ID_Data>false</bsvc:Include_External_Integration_ID_Data>
                </bsvc:Response_Group>
            </bsvc:Get_Applicants_Request>
        </wd:Get_Applicants_Request>
    </soapenv:Body>
</soapenv:Envelope>

我需要的xml如下所示

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:bsvc="urn:com.workday/bsvc">
    <soapenv:Header>
        <wsse:Security soapenv:mustUnderstand="1"
            xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
            xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
            <wsse:UsernameToken>
                <wsse:Username>Username</wsse:Username>
                <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">Password</wsse:Password>
            </wsse:UsernameToken>
        </wsse:Security>
    </soapenv:Header>
    <soapenv:Body>
        <bsvc:Get_Applicants_Request bsvc:version="v32.1">
            <bsvc:Request_Criteria>
                <bsvc:Email_Address>abc@gmail.com</bsvc:Email_Address>
            </bsvc:Request_Criteria>
            <bsvc:Response_Group>
                <bsvc:Include_Reference>true</bsvc:Include_Reference>
                <bsvc:Include_Personal_Information>true</bsvc:Include_Personal_Information>
                <bsvc:Include_Recruiting_Information>true</bsvc:Include_Recruiting_Information>
                <bsvc:Include_Qualification_Profile>true</bsvc:Include_Qualification_Profile>
                <bsvc:Include_Resume>false</bsvc:Include_Resume>
                <bsvc:Include_Background_Check>false</bsvc:Include_Background_Check>
                <bsvc:Include_External_Integration_ID_Data>false</bsvc:Include_External_Integration_ID_Data>
            </bsvc:Response_Group>
        </bsvc:Get_Applicants_Request>
    </soapenv:Body>
</soapenv:Envelope>

在soap 生成的xml 中,soapenv:envelope 中包含额外的命名空间。如何删除它。我在 soapenv:body 标签之后得到了额外的标签。

我正在传递以下标题和正文的详细信息。

标题:

let soapHeader = `<wsse:Security soapenv:mustUnderstand="1"
    xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
    xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
    <wsse:UsernameToken>
        <wsse:Username>Username</wsse:Username>
        <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">Password</wsse:Password>
    </wsse:UsernameToken>
</wsse:Security>`

和身体:

let xml = `<bsvc:Get_Applicants_Request bsvc:version="v32.1">
    <bsvc:Request_Criteria>
        <bsvc:Email_Address>abc@gmail.com</bsvc:Email_Address>
    </bsvc:Request_Criteria>
    <bsvc:Response_Group>
        <bsvc:Include_Reference>true</bsvc:Include_Reference>
        <bsvc:Include_Personal_Information>true</bsvc:Include_Personal_Information>
        <bsvc:Include_Recruiting_Information>true</bsvc:Include_Recruiting_Information>
        <bsvc:Include_Qualification_Profile>true</bsvc:Include_Qualification_Profile>
        <bsvc:Include_Resume>false</bsvc:Include_Resume>
        <bsvc:Include_Background_Check>false</bsvc:Include_Background_Check>
        <bsvc:Include_External_Integration_ID_Data>false</bsvc:Include_External_Integration_ID_Data>
    </bsvc:Response_Group>
</bsvc:Get_Applicants_Request>`

并使用以下方法使用soap请求xml,因为工作日提供soap api。

let client = await soap.createClientAsync(url, wsdlOptions)
client.addSoapHeader(soapHeader)
let resp = await client.Get_ApplicantsAsync(xml)

请帮助我根据需要使用 SOAP 包制作 xml。

【问题讨论】:

请看,这是工作日的HR apigithub.com/hikmat30ce/WorkdayIntegrator-HR 【参考方案1】:

您提供的代码示例未显示您作为方法参数提供的内容(代码中的 xml 对象是什么?)。

根据帖子,我假设您自己构建 xml 字符串并希望将其直接传递给 SOAP 包。在这种情况下,您的方法参数应如下所示(包文档中的“使用 XML 字符串作为参数的示例”)

const resp = await client.Get_ApplicantsAsync(_xml: xmlBody)

请记住,您缺少 bsvc 命名空间。 您可以直接在您的 xml 正文中将其添加到 Get_Applicants_Request 元素:

<bsvc:Get_Applicants_Request xmlns:bsvc="urn:com.workday/bsvc" bsvc:version="v32.1">
</bsvc:Get_Applicants_Request>

或者您可以将其包含在信封中(也许您还有其他调用也需要此命名空间):

client.wsdl.xmlnsInEnvelope += 'xmlns:bsvc="urn:com.workday/bsvc';

【讨论】:

我在github上创建了HR api,请看github.com/hikmat30ce/WorkdayIntegrator-HR

以上是关于如何更改node.js中soap包生成的xml?的主要内容,如果未能解决你的问题,请参考以下文章

需要 SOAP - Node JS 包失败并在 AWS Lambda 中出现导入错误

如何在 C# 中更改soap xml Web 服务?

C# |如何更改 XML 文档中的命名空间?

如何查看 PHP SOAP 客户端类生成的实际 XML?

使用java编写soap,想问一下如何查看(获取)server端和client端发出的xml格式的soap包

更改 XML 标记前缀 SOAP