how to read xml element in xml2js

Solutions on MaxInterview for how to read xml element in xml2js by the best coders in the world

showing results for - "how to read xml element in xml2js"
Nadine
21 Aug 2020
1var xml2js = require('xml2js');
2var xml = "<config><test>Hello</test><data>SomeData</data></config>";
3
4var extractedData = "";
5var parser = new xml2js.Parser();
6parser.parseString(xml, function(err,result){
7  //Extract the value from the data element
8  extractedData = result['config']['data'];
9  console.log(extractedData);
10});
11console.log("Note that you can't use value here if parseString is async; extractedData=", extractedData);