Skip to content

Instantly share code, notes, and snippets.

@AbuzerAsif
Last active May 11, 2019 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AbuzerAsif/09cd8be659ef8913ba75d21b8906b8ce to your computer and use it in GitHub Desktop.
Save AbuzerAsif/09cd8be659ef8913ba75d21b8906b8ce to your computer and use it in GitHub Desktop.
NodeJS XML to JSON using XML2JS https://embarkcode.com/nodejs-xml-to-json/
const xml2js = require('xml2js');
const productXML = '<Product><ID>10</ID><Name>Pizza</Name></Product>';
xml2js.parseString(productXML, { explicitArray : false }, function (err, result) {
// console.dir will allow us to print the whole object in our console
console.dir(result); // Output: { Product: { ID: '10', Name: 'Pizza' } }
console.dir(result.Product); // Output: { ID: '10', Name: 'Pizza' }
console.log(result.Product.ID); // Output: 10
console.log(result.Product.Name); // Output: Pizza
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment