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

Parsing XML to JSON in Amazon Lambda using external js libraries(使用外部 js 库在 Amazon Lambda 中将 XML 解析为 JSON)
本文介绍了使用外部 js 库在 Amazon Lambda 中将 XML 解析为 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

I am trying to convert an XML String that I get from a server to JSON inside my Lambda function.
I have set up this rather simple example to simulate the XML answer that i get from the server using DynamoDB. (Currently I'm just trying to get the convertion going)

'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);
            }

    });

}  ;

获取项目工作正常,并像这样返回

getting the item works just fine and is returned like this

{
  "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
  }
}

我现在的主要问题是我不知道如何导入任何 XMLtoJSON 库文件,例如 这里的这个

My main issue now is that I can not figure out how i can import any XMLtoJSON library files like this one here

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

I hope my code in this case is not completely worthless and there is a rather simple solution.

推荐答案

您正在经历许多新的 Lambda 用户所走过的道路.

You're going through the path that many new Lambda users have gone.

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

With Lambda, it is absolutely easy, you just write your code and validate that it works as expected - I mean on your computer.

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

Once you have validated it, do as follows:

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

如果您不小心也压缩了包含文件夹,那很好,只需确保更新 Lambda 以从以下位置运行脚本:dir_name/file_name.function_name(不要不要忘记从你的模块中导出 function_name).

If you accidentally zipped the containing folder as well, that is fine, just make sure to update Lambda to run the script from: dir_name/file_name.function_name (don't forget to export function_name from your module).

这篇关于使用外部 js 库在 Amazon Lambda 中将 XML 解析为 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)