it works for me
var xml2js = require('xml2js');
var xml = "<config><test>Hello</test><data>SomeData</data></config>";
var extractedData = "";
var parser = new xml2js.Parser();
parser.parseString(xml, function(err,result){
//Extract the value from the data element
extractedData = result['config']['data'];
console.log(extractedData);
});
console.log("Note that you can't use value here if parseString is async; extractedData=", extractedData);
result:
SomeData
Note that you can't use value here if parseString is async; extractedData= SomeData
Answer from Andrey Sidorov on Stack Overflow
» npm install xml2js
TL;DR
It's harder than it looks. Read the Open311 JSON and XML Conversion page for details of other JSON-side representations. All of them "use and abuse" arrays, extra layers of objects, members with names that didn't appear in the original XML, or all three.
Long Answer
xml2js has an un-enviable task: convert XML to JSON in a way that can be reversed, without knowing the schema in advance. It seems obvious, at first:
<name>Fred</name> → { name: "Fred" }
<chacha /> → { chacha: null }
Easy so far, right? How about this, though?
<x><y>z</y><x>
Removing the human friendly names drives home the uncertainty facing xml2js. At first, you might think this is quite reasonable:
{ x: { y: "z" } }
Later, you trip over this XML text and realise your guessed-at schema was wrong:
<x><y>z</y><y>z2</y></x>
Uh oh. Maybe we should have used an array. At least all the members have the same tag:
{ x: [ "z", "z2" ] }
Inevitably, though, that turns out to be short-sighted:
<x><y>z</y><y>z2</y><m>n</m>happy</x>
Uh...
{ x: [ { y: "z" }, { y : "z2" }, { m: "n" }, "happy" ] }
... and then someone polishes you off with some attributes and XML namespaces.
The way to construct a more concise output schema feels obvious to you. You can infer details from the tag and attribute names. You understand it.
The library does not share that understanding.
If the library doesn't know the schema, it must either "use and abuse" arrays, extra layers of objects, special attribute names, or all three.
The only alternative is to employ a variable output schema. That keeps it simple at first, as we saw above, but you'll quickly find yourself writing a great deal of conditional code. Consider what happens if children with the same tag name are collapsed into a list, but only if there are more than one:
if (Array.isArray(x.y)) {
processTheYChildren(x.y);
} else if (typeof(x.y) === 'object') {
// only one child; construct an array on the fly because my converter didn't
processTheYChildren([x.y]);
} else ...
As xml2js' documentation states, you can configure the parser to not abuse of arrays, by setting the property explicitArray to false (important: it has to be a boolean value as the string "false" will just not work!)
Example:
var parser = new xml2js.Parser({explicitArray : false});
This way, you should be able to access your JSON properties in a much easier way. I hope this helps anyone.
» npm install xml2js-parser
» npm install @types/xml2js