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
npmjs.com › package › xml2js
xml2js - npm
Simple XML to JavaScript object converter.. Latest version: 0.6.2, last published: 3 years ago. Start using xml2js in your project by running `npm i xml2js`. There are 10760 other projects in the npm registry using xml2js.
      » npm install xml2js
    
Published   Jul 26, 2023
Version   0.6.2
Author   Marek Kubica
🌐
Npmdoc
npmdoc.github.io › node-npmdoc-xml2js › build › apidoc.html
api documentation for xml2js (v0.4.17)
... That's right, if you have been using xml-simple or a home-grown wrapper, this was added in 0.1.11 just for you: ```javascript var fs = require('fs'), xml2js = require('xml2js'); var parser = new xml2js.Parser(); fs.readFile(__dirname + '/foo.xml', function(err, data) { parser.parseString(data, ...
🌐
Tabnine
tabnine.com › home page › code › javascript › xml2js
xml2js JavaScript and Node.js code examples | Tabnine
xml2js.parseString(input, function (err, obj) { if (err) { return reject(err); } resolve(obj); }); origin: produck/cas-client · parseString(xmlString, { explicitRoot: false, tagNameProcessors: [stripPrefix] }, (error, result) => { if (error) { return reject(error); } resolve(result); }); origin: dbusjs/node-dbus-next ·
🌐
Snyk
snyk.io › advisor › xml2js › xml2js code examples
Top 5 xml2js Code Examples | Snyk
April 6, 2023 - export async function getFailures(log: ToolingLog, testReportPath: string) { const xml = await readAsync(testReportPath, 'utf8'); // Parses junit XML files const report: TestReport = await xml2js.parseStringPromise(xml); // Grab the failures. Reporters may report multiple testsuites in a single file.
Top answer
1 of 6
59

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 ...
2 of 6
52

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.

🌐
IronPDF
ironpdf.com › ironpdf for node.js › ironpdf for node.js blog › node help › xml2js npm
xml2js npm (How It Works For Developers)
June 23, 2025 - A Node.js package called XML2JS makes it easier to parse and create a simple XML (Extensible Markup Language) to JavaScript object converter. By offering ways to parse XML files or texts and convert them into structured JavaScript objects, it ...
🌐
CloudDefense.ai
clouddefense.ai › code › javascript › example › xml2js
Top 10 Examples of <!-- -->xml2js<!-- --> code in Javascript | CloudDefense.AI
export async function getFailures(log: ToolingLog, testReportPath: string) { const xml = await readAsync(testReportPath, 'utf8'); // Parses junit XML files const report: TestReport = await xml2js.parseStringPromise(xml); // Grab the failures. Reporters may report multiple testsuites in a single file.
Find elsewhere
🌐
CodeSignal
codesignal.com › learn › courses › hierarchical-and-structured-data-formats-1 › lessons › working-with-xml-in-javascript-using-xml2js
Working with XML in JavaScript using xml2js
When xml2js parses XML into a JavaScript object, it converts elements into properties of the object, handling attributes and child elements efficiently. Attributes of an XML element are represented as a separate property named "$" within the object.
🌐
UNPKG
unpkg.com › browse › xml2js@0.4.0 › README.md
xml2js
If you like to contribute, keep in mind that xml2js is written in CoffeeScript, so don't develop on the JavaScript files that are checked into the repository for convenience reasons. Also, please write some unit test to check your behaviour and if it is some user-facing thing, add some documentation ...
🌐
GitHub
github.com › Leonidas-from-XIV › node-xml2js
GitHub - Leonidas-from-XIV/node-xml2js: XML to JavaScript object converter. · GitHub
XML to JavaScript object converter. Contribute to Leonidas-from-XIV/node-xml2js development by creating an account on GitHub.
Starred by 5K users
Forked by 612 users
Languages   CoffeeScript
🌐
npm
npmjs.com › package › xml2js-parser
xml2js-parser - npm
November 16, 2016 - Simple XML to JavaScript object converter.. Latest version: 1.1.1, last published: 9 years ago. Start using xml2js-parser in your project by running `npm i xml2js-parser`. There are 43 other projects in the npm registry using xml2js-parser.
      » npm install xml2js-parser
    
Published   Nov 16, 2016
Version   1.1.1
Author   Lauris Vavere
🌐
RubyDoc
rubydoc.info › gems › xml2js
File: README — Documentation for xml2js (0.1.1)
Xml2js is intended to be a wrapper around the excellent xml2js node package. While at my previous company we ran into many instances where current ruby xml parsers just simply did not capture everything in the long xml files that we used. The node version did.
🌐
Readthedocs
rgi-assessment-tool.readthedocs.io › en › stable › node_modules › s3 › node_modules › aws-sdk › node_modules › xml2js › README.html
node-xml2js — rgi-assessment-tool stable documentation
Ever had the urge to parse XML? And wanted to access the data in some sane, easy way? Don’t want to compile a C parser, for whatever reason? Then xml2js is what you’re looking for!
🌐
UNPKG
unpkg.com › browse › xml2js@0.2.3 › README.md
xml2js/README.md
If you like to contribute, keep in mind that xml2js is written in CoffeeScript, so don't develop on the JavaScript files that are checked into the repository for convenience reasons. Also, please write some unit test to check your behaviour and if it is some user-facing thing, add some documentation ...
🌐
npm
npmjs.com › package › @types › xml2js
@types/xml2js - npm
November 7, 2023 - TypeScript definitions for xml2js. Latest version: 0.4.14, last published: 2 years ago. Start using @types/xml2js in your project by running `npm i @types/xml2js`. There are 369 other projects in the npm registry using @types/xml2js.
      » npm install @types/xml2js
    
🌐
GitHub
github.com › vavere › xml2js-parser
GitHub - vavere/xml2js-parser: Simple XML to JavaScript object converter
Simple XML to JavaScript object converter. Contribute to vavere/xml2js-parser development by creating an account on GitHub.
Author   vavere
🌐
GitHub
github.com › Leonidas-from-XIV › node-xml2js › issues › 694
xml2js documentation/changelogs · Issue #694 · Leonidas-from-XIV/node-xml2js
August 21, 2023 - xml2js documentation/changelogs#694 · Copy link · KRAUS1203 · opened · on Aug 21, 2023 · Issue body actions · Is there any changelogs for this package as I currently have 0.4.23 and want to upgrade to latest version but I dont see any changelogs for these new releases?
Published   Aug 21, 2023
🌐
npm
npmjs.com › search
xml2js - npm search
Convert an XML file or XML data to JSON (via xml2js), with promises.