5

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. Commented Jun 28, 2017 at 23:07

1 Answer 1

10

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This appears to only work with the now deprecated Node 8 runtime, and not Node 10. Any thoughts on what the problem is or how to remedy it?
Yeah it's broken now

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.