Last active
May 11, 2019 08:33
-
-
Save AbuzerAsif/770a886d15761e97268dbf08bb87c2e9 to your computer and use it in GitHub Desktop.
NodeJS XML to JSON using XML2JS https://embarkcode.com/nodejs-xml-to-json/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const xml2js = require('xml2js'); | |
const productXML = '<Product><ID>10</ID><Name>Pizza</Name></Product>'; | |
xml2js.parseString(productXML, 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[0]); // Output: 10 | |
console.log(result.Product.Name[0]); // Output: Pizza | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment