使用外部 js 库在 Amazon Lambda 中将 XML 解析为 JSON

Parsing XML to JSON in Amazon Lambda using external js libraries

我正在尝试将从服务器获取的 XML 字符串转换为我的 Lambda 函数中的 JSON。
我已经设置了这个相当简单的示例来模拟我使用 DynamoDB 从服务器获得的 XML 答案。 (目前我只是想让转换继续进行)

'use strict';

var AWS = require('aws-sdk');
var docClient = new AWS.DynamoDB.DocumentClient({region: 'eu-west-1'});


exports.handler = function (e, ctx, callback){    
    let table = "dsbTable";
    let bpNumber = 1337;
    var test;
    var x2js = new X2JS();
    let params = {
            TableName: table,
            Key:{
                "bpNumber": bpNumber
            },
        };
    docClient.get(params, function(err, data) {
            if (err) {
                console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
                callback(err, null);
            } else {
                console.log("GetItem succeeded:", JSON.stringify(data, null, 2));
                console.log('test' +data.Item.getBp);
                //var jsonObj = x2js.xml_str2json(data.Item.getBp);
                //console.log(jsonObj);

                callback(null, data);
            }

    });

}  ;

获取商品效果很好,返回方式如下

{
  "Item": {
    "getBp": "<message version=\"1.0\" system=\"AVS/3\"><header><client>553</client><avs3-sales-organization>7564</avs3-sales-organization><avs3-service-provider>DSD</avs3-service-provider></header><body><business-partner><salutation-code>01</salutation-code><titel-code-academic/><titel-academic/><titel-code-royal/><titel-royal/><job-titel/><last-name1>Pickle</last-name1><last-name2/><first-name>N</first-name><street/><street-suffix/><street-number/><street-number-suffix/><address-line-1>10 Waterside Way</address-line-1><address-line-2/><address-line-3/><zipcode>NN4 7XD</zipcode><country-code>GB</country-code><city>NORTHAMPTON</city><district/><region-code>NH</region-code><region-text>Northamptonshire</region-text><company1/><company2/><company3/><department/><po-box/><po-box-zipcode/><po-box-city/><po-box-country-code/><major-customer-zipcode/><address-source/><advertisement>Y</advertisement><category/><bp-number>1100000772</bp-number><bp-number-external/><bp-group>ABON</bp-group><eu-sales-tax-number/><bic-master-number/><sector/><communication><communication-type>WW</communication-type><communication-value>kate.southorn@dsbnet.co.uk</communication-value><communication-default>Y</communication-default></communication><attribute><attribute-type>ACC</attribute-type><attribute-value>Y</attribute-value></attribute><attribute><attribute-type>OIEMEX</attribute-type><attribute-value>N20121211</attribute-value></attribute><attribute><attribute-type>OINLIN</attribute-type><attribute-value>N20121211</attribute-value></attribute><attribute><attribute-type>OISMEX</attribute-type><attribute-value>N20121211</attribute-value></attribute><attribute><attribute-type>OISMIN</attribute-type><attribute-value>N20121211</attribute-value></attribute><attribute><attribute-type>OOEMIN</attribute-type><attribute-value>N20121211</attribute-value></attribute><attribute><attribute-type>OOFXEX</attribute-type><attribute-value>N20121211</attribute-value></attribute><attribute><attribute-type>OOFXIN</attribute-type><attribute-value>N20121211</attribute-value></attribute><attribute><attribute-type>OOPTEX</attribute-type><attribute-value>N20121211</attribute-value></attribute><attribute><attribute-type>OOPTIN</attribute-type><attribute-value>N20121211</attribute-value></attribute><attribute><attribute-type>OOTEEX</attribute-type><attribute-value>N20121211</attribute-value></attribute><attribute><attribute-type>OOTEIN</attribute-type><attribute-value>N20121211</attribute-value></attribute><attribute><attribute-type>THEDSU</attribute-type><attribute-value/></attribute></business-partner></body></message>",
    "bpNumber": 1337
  }
}

我现在的主要问题是我不知道如何导入任何 XML 到 JSON 库文件,例如 this one here

我希望我的代码在这种情况下不是完全没有价值,并且有一个相当简单的解决方案。

您正在经历许多 Lambda 新用户走过的路。

使用 Lambda,这绝对简单,您只需编写代码并验证它是否按预期工作 - 我的意思是在您的计算机上

验证后,请执行以下操作:

  1. 压缩整个文件夹的内容,包括 node_modules目录和您使用的任何依赖项
  2. 将其上传到 Lambda。

如果您不小心也压缩了包含的文件夹,没关系,只需确保将 Lambda 更新为 运行 脚本来自:dir_name/file_name.function_name (不要忘记从您的模块中导出 function_name)。

始终处理程序名称是 <filename>.<handler> 函数名称>,如果 filename 被错误提及,那么也会在 cloudwatch 日志中抛出此类错误。