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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With