Is it possible to do XML parsing in an AWS Node.js Lambda function without using a 3rd party module like xml2js? I'm wondering if AWS has any built-in functionality for this like in the AWS SDK for Node.js.
1
-
I don't think they have it. From my experience, if speed is not critical, best to stick with a JS implementation so you can push from your development machine; otherwise, if you need native modules; should built it and deploy from CI.Tuan Anh Tran– Tuan Anh Tran2017-06-28 23:07:15 +00:00Commented Jun 28, 2017 at 23:07
Add a comment
|
1 Answer
Actually I just tested this and you can actually use xml2js straight out of the box because...
https://github.com/aws/aws-sdk-js/blob/master/lib/xml/node_parser.js
That's what the AWS JS SDK uses. Sample Lambda code use to test this, completely using the Lambda online editor and running test data against it:
'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');
};
That said if you want to avoid that route you're going to have to go the standard package install route.