node-xml2js

XML-to-JavaScript-object parser and builder for Node.js

Library
npm
v0.6.2
4,960 stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
46 /100 Fair
Development Activity 4
Maintenance 0
Community 80
Maturity 60
Momentum 40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
66 /100 Good
Architecture 70
Code Quality 65
Innovation 55
Learning Curve 72

xml2js is a long-standing Node.js library that converts XML documents into plain JavaScript objects and can build XML back from JavaScript data structures. It wraps the SAX-based sax parser under a simpler, callback/promise-based API, exposing configuration options for common XML parsing headaches: attribute handling, namespace resolution, explicit-array normalization, whitespace trimming, and custom tag/attribute name processors.

Because raw XML-to-object conversion is ambiguous (should a repeated child element become an array? should attributes be nested under a special key or merged into the object?), xml2js’s large set of parser options exists specifically to let consumers pick the shape that matches how they intend to use the resulting object, rather than imposing one opinionated mapping. This flexibility, combined with its long track record and near-ubiquitous adoption in the Node ecosystem, is why it remains the default choice for parsing SOAP responses, RSS/Atom feeds, config files, and other legacy XML formats in JavaScript backends.

What You Get

  • A Parser class (and shorthand parseString/parseStringPromise functions) that converts an XML string into a plain JavaScript object
  • A Builder class that serializes a JavaScript object back into an XML string, using xmlbuilder internally
  • Configurable attribute handling (explicitArray, mergeAttrs, attrkey/charkey) to control the exact shape of the resulting object
  • XML namespace support, including stripping or preserving namespace prefixes during parsing
  • Custom tagNameProcessors/attrNameProcessors/valueProcessors hooks for transforming names and values during parsing

Common Use Cases

  • Parsing SOAP API responses into JavaScript objects for further processing
  • Reading legacy XML configuration files (e.g. web.config, Maven pom.xml-style files) in Node.js tooling
  • Converting RSS/Atom XML feeds into JSON-like structures for feed readers or aggregators
  • Round-tripping XML in migration or ETL scripts that need to modify a document and re-serialize it
  • Interfacing with older enterprise systems and APIs that only expose XML endpoints

Under The Hood

Architecture The library is a thin, well-organized wrapper around two lower-level packages: sax for the actual XML tokenizing/parsing and xmlbuilder for serialization. lib/parser.js (the largest file, ~395 lines) implements the Parser class, which drives a sax stream and, on each SAX event (open tag, text, close tag), incrementally builds up the target JavaScript object according to the active options; lib/builder.js mirrors this in reverse, walking a JS object and emitting an xmlbuilder document. lib/defaults.js centralizes the option presets for the two supported parsing “modes” (0.1 legacy defaults vs 0.2 current defaults), and lib/processors.js implements the built-in name/value processor functions (e.g. stripPrefix, parseNumbers) that users can opt into.

Tech Stack Source is written in CoffeeScript (src/) and compiled to the published CommonJS JavaScript (lib/) via a Cakefile build script; the only runtime dependencies are sax (XML tokenizing) and xmlbuilder (XML serialization) — both mature, minimal libraries.

Code Quality Tests are written in CoffeeScript (test/*.test.coffee) covering the parser, builder, BOM-handling, and processor functions, run via nyc/zap with Coveralls coverage reporting wired into CI. Since core logic is compiled from CoffeeScript rather than hand-written JS, contributors need CoffeeScript familiarity to modify lib/ directly (or must edit src/ and rebuild), which is an unusual but consistent pattern for a project this size and age.

API Design The primary entry points — parseString(xml, callback), parseStringPromise(xml), and new Builder().buildObject(obj) — are simple and immediately usable with zero configuration, while the large options object (explicitArray, mergeAttrs, explicitRoot, attrkey, etc.) is layered on top for cases needing precise control over the JS-object shape. This two-tier design (simple defaults, deep configurability) is why it has remained a stable dependency for over a decade despite XML parsing inherently requiring many edge-case decisions.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers