是否可以在不使用 xml2js 等第三方模块的情况下在 AWS Node.js Lambda 函数中进行 XML 解析?我想知道 AWS 是否有任何内置功能,例如 AWS SDK for Node.js。
最佳答案
实际上我刚刚对此进行了测试,您实际上可以直接使用 xml2js,因为...
https://github.com/aws/aws-sdk-js/blob/master/lib/xml/node_parser.js
这就是 AWS JS SDK 所使用的。示例 Lambda 代码用于对此进行测试,完全使用 Lambda 在线编辑器并针对它运行测试数据:
'use strict';
var xml2js = require('xml2js');
console.log('Loading function');
var options = { // options passed to xml2js parser
explicitCharkey: false, // undocumented
trim: false, // trim the leading/trailing whitespace from text nodes
normalize: false, // trim interior whitespace inside text nodes
explicitRoot: false, // return the root node in the resulting object?
emptyTag: null, // the default value for empty nodes
explicitArray: true, // always put child nodes in an array
ignoreAttrs: false, // ignore attributes, only create text nodes
mergeAttrs: false, // merge attributes and child elements
validator: null // a callable validator
};
exports.handler = (event, context, callback) => {
var parser = new xml2js.Parser(options);
//console.log('Received event:', JSON.stringify(event, null, 2));
console.log('value1 =', event.key1);
console.log('value2 =', event.key2);
console.log('value3 =', event.key3);
callback(null, event.key1); // Echo back the first key value
//callback('Something went wrong');
};
也就是说,如果您想避开那条路线,您将不得不走 the standard package install route .
关于node.js - AWS Lambda 函数中的 XML 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44811982/