You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NodeJS中从SOAP XML响应提取ArrayRegistration数组的技术问询

提取SOAP响应中的ArrayRegistration数组(Node.js实现)

你在Node.js开发SOAP客户端时,需要从响应中提取ArrayRegistration数组,以下分别针对node-soap和easy-soap-request两种工具给出具体实现方案:


一、使用node-soap的解决方案

node-soap会自动将SOAP响应解析为JavaScript对象,可直接通过对象属性层级访问ArrayRegistration。注意响应中的命名空间(http://www.w3.org/2001/04/xmlenc#)可能影响解析后的对象结构。

示例代码

const soap = require('soap');

const url = '你的SOAP服务WSDL地址';
const authHeader = {
  AuthenticationHeader: {
    UserName: 'user',
    Password: 'password'
  }
};

soap.createClient(url, (err, client) => {
  if (err) {
    console.error(err);
    return;
  }

  // 设置SOAP Header
  client.addSoapHeader(authHeader, '', 'xe', 'http://www.w3.org/2001/04/xmlenc#');

  // 调用GetRegistration方法
  const args = { id: '123456' };
  client.GetRegistration(args, (err, result) => {
    if (err) {
      console.error(err);
      return;
    }

    // 提取ArrayRegistration数组,根据WSDL定义调整层级
    const arrayRegistration = result.GetRegistrationResult.ArrayRegistration;
    console.log('提取到的ArrayRegistration数组:', arrayRegistration);

    // 后续解密处理逻辑...
  });
});

说明

  • 若WSDL定义中ArrayRegistration为数组类型,node-soap会直接解析为JS数组;空节点(如示例中的<ArrayRegistration />)可能返回空数组或undefined,需做判空处理。
  • 可通过client.describe()查看服务方法结构,确认属性路径是否正确。

二、使用easy-soap-request的解决方案

easy-soap-request仅返回原始XML响应,需借助xml2js等XML解析库转换为JS对象后提取目标数组。

步骤1:安装依赖

npm install easy-soap-request xml2js

示例代码

const soapRequest = require('easy-soap-request');
const xml2js = require('xml2js');

const url = '你的SOAP服务端点地址';
const headers = {
  'Content-Type': 'application/soap+xml; charset=utf-8'
};

// 构造请求XML
const xml = `
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xe="http://www.w3.org/2001/04/xmlenc#">
  <soap:Header>
    <xe:AuthenticationHeader>
      <xe:UserName>user</xe:UserName>
      <xe:Password>password</xe:Password>
    </xe:AuthenticationHeader>
  </soap:Header>
  <soap:Body>
    <xe:GetRegistration>
      <xe:id>123456</xe:id>
    </xe:GetRegistration>
  </soap:Body>
</soap:Envelope>
`;

// 发送SOAP请求
soapRequest({ url, headers, xml })
  .then(({ response }) => {
    const { body } = response;

    // 解析XML为JS对象
    xml2js.parseString(body, { explicitArray: false }, (err, result) => {
      if (err) {
        console.error(err);
        return;
      }

      // 层级遍历提取ArrayRegistration
      const envelope = result['soap:Envelope'];
      const body = envelope['soap:Body'];
      const responseData = body.GetRegistrationResponse;
      const resultData = responseData.GetRegistrationResult;
      const arrayRegistration = resultData.ArrayRegistration;

      // 处理空节点情况:将空对象转为空数组
      const finalArray = Array.isArray(arrayRegistration) ? arrayRegistration : [];
      console.log('提取到的ArrayRegistration数组:', finalArray);

      // 后续解密处理逻辑...
    });
  })
  .catch(err => console.error(err));

说明

  • xml2js的explicitArray: false配置会将单个元素转为对象、多个元素转为数组,需额外判断类型避免空节点导致的异常。
  • 若ArrayRegistration包含子元素,解析后会自动转为数组结构。

附:参考资源

请求XML示例

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xe="http://www.w3.org/2001/04/xmlenc#">
<soap:Header>
  <xe:AuthenticationHeader>
     <!--Optional:-->
     <xe:UserName>user</xe:UserName>
     <!--Optional:-->
     <xe:Password>password</xe:Password>
  </xe:AuthenticationHeader>
</soap:Header>
<soap:Body>
  <xe:GetRegistration>
     <!--Optional:-->
     <xe:id>123456</xe:id>
  </xe:GetRegistration>
</soap:Body>
</soap:Envelope>

响应XML示例

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetRegistrationResponse xmlns="http://www.w3.org/2001/04/xmlenc#"> 
<GetRegistrationResult>
<ErrorInQuery>false</ErrorInQuery>
<ErrorInfo>Success!</ErrorInfo>
<ArrayRegistration />
</GetRegistrationResult>
</GetRegistrationResponse>
</soap:Body>
</soap:Envelope>

C#参考实现

public partial class RRegistration : object, System.ComponentModel.INotifyPropertyChanged {
    
    private bool errorQuery;
    
    private string errorInformation;
    
    private Registration[] arrayRegistrationField;
    
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order=0)]
    public bool ErrorInQuery {
        get {
            return this.errorQuery;
        }
        set {
            this.errorQuery = value;
            this.RaisePropertyChanged("ErrorQuery");
        }
    }
    
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order=1)]
    public string ErrorInformation {
        get {
            return this.errorInformation;
        }
        set {
            this.errorInformation = value;
            this.RaisePropertyChanged("ErrorInformation");
        }
    }
    
    /// <remarks/>
    [System.Xml.Serialization.XmlArrayAttribute(Order=2)]
    public Registration[] ArrayRegistration {
        get {
            return this.arrayRegistrationField;
        }
        set {
            this.arrayRegistrationField = value;
            this.RaisePropertyChanged("ArrayRegistration");
        }
    }
    
    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
    
    protected void RaisePropertyChanged(string propertyName) {
        System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
        if ((propertyChanged != null)) {
            propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }
}

内容的提问来源于stack exchange,提问作者BREAKWAVE

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 21:10:37