Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML parsing in AWS Lambda function

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.

like image 844
Berry Blue Avatar asked Jun 28 '17 20:06

Berry Blue


1 Answers

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.

like image 121
Chris White Avatar answered Oct 09 '22 20:10

Chris White