If you set the xplicitArray option to false, when it parses it won't create those arrays where you didn't expect. Like this:
function parseBody(body) {
var parseString = require('xml2js').parseString;
const options = {
explicitArray: false
};
var xml = body
parseString(xml, options, function (err, result) {
console.log("cust fname: " + result['soapenv:Envelope']['soapenv:Body']['int:readResponse']['response']['cust']['fName']);
})
}
var input = '<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:int=\"realURLHere"><soapenv:Header/><soapenv:Body><int:readResponse><response><request>?</request><cust><fName>?</fName></cust></response></int:readResponse></soapenv:Body></soapenv:Envelope>';
parseBody(input);
Furthermore if you remove the prefixes it can get even cleaner:
function parseBody(body) {
var parseString = require('xml2js').parseString;
var stripNS = require('xml2js').processors.stripPrefix;
const options = {
tagNameProcessors: [stripNS],
explicitArray: false
};
var xml = body
parseString(xml, options, function (err, result) {
console.log("cust fname: " + result.Envelope.Body.readResponse.response.cust.fName);
})
}
var input = '<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:int=\"realURLHere"><soapenv:Header/><soapenv:Body><int:readResponse><response><request>?</request><cust><fName>?</fName></cust></response></int:readResponse></soapenv:Body></soapenv:Envelope>';
parseBody(input);```
Answer from Marc Pereira on Stack Overflownpm
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 10737 other projects in the npm registry using xml2js.
» npm install xml2js
Published Jul 26, 2023
Version 0.6.2
Author Marek Kubica
node.js - Parsing with xml2js - Stack Overflow
I am new to node.js and need to parse XML. In js, I just use a DOMParser and use getElementsByTagName to get the values I desire. I have just switched to node and am using xml2js (willing to consider More on stackoverflow.com
Using xml2js gives me an odd result
I’m using the xml2js library but seem to be getting an odd console log which looks like it may be proxy information instead of the actual XML response (which is returned correctly). I’m not using a proxy. Console Log looks like this Feeling a bit bewildered but very open to the possibility ... More on community.postman.com
[RC0] How to import "xml2js"
After RC0 upgrade the xml2js import failed : npm install xml2js --save npm install @types/xml2js --save --save-exact the code : import xml2js from "xml2js"; @Injectable() export class XMLDataService { construct… More on forum.ionicframework.com
Having difficulty with xml2js on Xml->Json->Xml in NodeJS
You're passing stringified JSON back into the buildObject() function and not a javascript object. After your .replace() call you should do a JSON.parse() before passing it into buildObject(). Also, your .replace() will only replace the first instance of "Hello" and not all instances of it. You should use a global regex instead: var needle = /Hello/g; json = JSON.stringify( json ).replace( needle, "Hellow" ); More on reddit.com
Videos
06:01
Accelerate XML to JSON Transformation with Node JS [xml2js] ...
05:56
Parsing XML to JSON in Nodejs || XML to JSON in NodeJs || XML || ...
How to Use xml2js in ES6: Parsing XML to JavaScript Objects ...
02:09
Converting XML to Object and Object to XML in Node.js - YouTube
npm
npmjs.com › search
xml2js - npm search
Convert an XML file or XML data to JSON (via xml2js), with promises.
Top answer 1 of 4
4
If you set the xplicitArray option to false, when it parses it won't create those arrays where you didn't expect. Like this:
function parseBody(body) {
var parseString = require('xml2js').parseString;
const options = {
explicitArray: false
};
var xml = body
parseString(xml, options, function (err, result) {
console.log("cust fname: " + result['soapenv:Envelope']['soapenv:Body']['int:readResponse']['response']['cust']['fName']);
})
}
var input = '<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:int=\"realURLHere"><soapenv:Header/><soapenv:Body><int:readResponse><response><request>?</request><cust><fName>?</fName></cust></response></int:readResponse></soapenv:Body></soapenv:Envelope>';
parseBody(input);
Furthermore if you remove the prefixes it can get even cleaner:
function parseBody(body) {
var parseString = require('xml2js').parseString;
var stripNS = require('xml2js').processors.stripPrefix;
const options = {
tagNameProcessors: [stripNS],
explicitArray: false
};
var xml = body
parseString(xml, options, function (err, result) {
console.log("cust fname: " + result.Envelope.Body.readResponse.response.cust.fName);
})
}
var input = '<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:int=\"realURLHere"><soapenv:Header/><soapenv:Body><int:readResponse><response><request>?</request><cust><fName>?</fName></cust></response></int:readResponse></soapenv:Body></soapenv:Envelope>';
parseBody(input);```
2 of 4
2
var custFName = result['soapenv:Envelope']['soapenv:Body'][0]['int:realURLHere'][0]['response'][0]['cust'][0]['fName'];
This solved my problem. My struggles had to do with this response having everything in an array. If there is an easier way to do this or something more dynamic, please let me know. But for now, this worked.
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.
Generalist Programmer
generalistprogrammer.com › home › tutorials › npm packages › xml2js: simple xml to javascript object converte guide
Xml2js Guide: Simple XML to JavaScript object converte [2025]
November 16, 2025 - xml2js is a Simple XML to JavaScript object converter..
GitHub
github.com › RikkiGibson › isomorphic-xml2js
RikkiGibson/isomorphic-xml2js: XML parsing for ...
A drop-in replacement for xml2js that uses the built in DOMParser/XMLSerializer when bundled for the browser, drastically reducing the bundle size (~129k for node-xml2js, currently ~6k for isomorphic-xml2js).
Author RikkiGibson
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
SourceForge
sourceforge.net › projects › xml2js.mirror
xml2js download | SourceForge.net
August 21, 2025 - xml2js is a Node.js module that converts XML into JavaScript objects (and vice versa). It simplifies XML parsing by using pure JavaScript and supports both synchronous and asynchronous parsing.
Debian
packages.debian.org › sid › node-xml2js
Debian -- Details of package node-xml2js in sid
xml2js parses XML using node-sax and converts it to a plain JavaScript object. Node.js is an event-based server-side javascript engine. dep: node-diff · javascript text differencing implementation · dep: node-sax · event-based streaming XML parser - Node.js module ·
npm
npmjs.com › package › xml2js-xpath
xml2js-xpath - npm
April 25, 2022 - A library for node-xml2js that allows querying the JSON object with XPath syntax.
» npm install xml2js-xpath
Published Apr 25, 2022
Version 0.13.0
Author Dane Summers
Gossinteractive
docs.gossinteractive.com › article › 7347 › xml2js
xml2js - The Digital Platform Documentation Site
Parses xml and converts xml to and from JavaScript objects.
UNPKG
app.unpkg.com › xml2js@0.4.0 › files › README.md
xml2js
Simple XML to JavaScript object converter. ... node-xml2js =========== 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! Description =========== Simple XML to ...
Postman
community.postman.com › help hub
Using xml2js gives me an odd result - Help Hub - Postman Community
July 4, 2025 - I’m using the xml2js library but seem to be getting an odd console log which looks like it may be proxy information instead of the actual XML response (which is returned correctly). I’m not using a proxy. Console Log looks like this Feeling a bit bewildered but very open to the possibility ...
Qite
nuget.qite.be › feeds › NPM › xml2js › versions
xml2js Versions
To install xml2js, run the following command after configuring npm:npm install xml2js@0.6.2 · To install xml2js, run the following command after configuring npm:yarn add xml2js@0.6.2 · Install with npm · Add with yarn · configuration help · To install xml2js, run the following command after configuring npm:npm install xml2js@0.4.1-4.1 ·