fast-xml-parser, xml-js, and xml2json are libraries designed to convert XML data into JSON format and vice versa within JavaScript environments. fast-xml-parser focuses on high performance and validation capabilities, making it suitable for large datasets. xml-js offers flexible conversion options, supporting both compact and standard JSON structures. xml2json is an older utility often used for simple transformations but lacks the active maintenance and feature depth of the other two.
When working with XML in modern JavaScript applications, you need a reliable way to convert data into JSON for easier handling. fast-xml-parser, xml-js, and xml2json all solve this problem, but they differ in speed, features, and long-term support. Let's compare how they handle real-world tasks.
fast-xml-parser is built for speed. It uses a single class to handle parsing and offers synchronous processing by default.
// fast-xml-parser: Fast synchronous parsing
const { XMLParser } = require("fast-xml-parser");
const parser = new XMLParser();
const result = parser.parse(xmlString);
xml-js provides a flexible convert function that handles options for attributes and text nodes.
// xml-js: Flexible conversion
const convert = require('xml-js');
const result = convert.xml2json(xmlString, { compact: false });
xml2json typically uses a callback pattern or simple function call, often wrapping older parsing logic.
// xml2json: Simple callback style
const xml2json = require('xml2json');
xml2json.parser(xmlString, function(err, result) {
const json = JSON.parse(result);
});
How the library structures the JSON output matters for your downstream code.
fast-xml-parser gives you a clean standard object by default but lets you tweak attribute handling.
// fast-xml-parser: Custom attribute prefix
const parser = new XMLParser({
ignoreAttributes: false,
attributeNamePrefix: "@_"
});
const result = parser.parse(xmlString);
xml-js shines here with a "compact" mode that keeps the structure close to the XML tree.
// xml-js: Compact mode
const result = convert.xml2json(xmlString, { compact: true });
// Output: { root: { child: { _text: "value" } } }
xml2json usually outputs a fixed structure based on the underlying engine, with less control over shape.
// xml2json: Fixed structure
xml2json.parser(xmlString, function(err, result) {
// Structure depends on internal implementation
console.log(result);
});
Processing untrusted XML can be risky. Validation helps prevent errors or security issues.
fast-xml-parser includes a built-in validator to check XML before parsing.
// fast-xml-parser: Built-in validation
const { XMLValidator } = require("fast-xml-parser");
const isValid = XMLValidator.validate(xmlString);
if (isValid === true) { /* safe to parse */ }
xml-js relies on the underlying parser and does not offer explicit validation steps in its main API.
// xml-js: No built-in validation
// You must try-catch the convert function
try {
convert.xml2json(xmlString);
} catch (e) { /* handle error */ }
xml2json provides basic error handling via callbacks but lacks advanced validation features.
// xml2json: Callback error handling
xml2json.parser(xmlString, function(err, result) {
if (err) { /* handle error */ }
});
Sometimes you need to send data back as XML, not just read it.
fast-xml-parser includes a dedicated builder class for this task.
// fast-xml-parser: JSON to XML
const { XMLBuilder } = require("fast-xml-parser");
const builder = new XMLBuilder();
const xmlOutput = builder.build(jsonObject);
xml-js handles this with the same convert function using a different method name.
// xml-js: JSON to XML
const xmlOutput = convert.json2xml(jsonObject, { compact: false });
xml2json is primarily focused on parsing and often lacks a robust builder for the reverse process.
// xml2json: Limited build support
// Often requires separate library or manual string construction
Choosing a library means trusting its maintainers to fix bugs and keep up with Node.js changes.
fast-xml-parser is actively maintained with frequent updates and strong community support.
// fast-xml-parser: Active ecosystem
// Regular npm updates and issue resolution
xml-js is also well-maintained and widely used in production environments.
// xml-js: Stable maintenance
// Consistent updates for compatibility
xml2json has seen little activity in recent years and may not support modern Node.js features.
// xml2json: Legacy status
// Check npm page for last publish date before using
Despite their differences, these libraries share common goals and basic behaviors.
// All support basic XML input
const xml = "<root>data</root>";
// parser.parse(xml) or convert.xml2json(xml)
// All accept options objects
const options = { ignoreAttributes: true };
// parser = new XMLParser(options) or convert.xml2json(xml, options)
// All work in Node.js
const lib = require('package-name');
// All require try-catch or callbacks
try { /* parse */ } catch (e) { /* handle */ }
// Check package.json for dependencies
// "dependencies": {}
| Feature | Shared by All Three |
|---|---|
| Core Function | π XML β JSON Conversion |
| Environment | π Node.js & Browser |
| Config | βοΈ Options for attributes |
| Safety | β Error handling support |
| Weight | π¦ Lightweight core |
| Feature | fast-xml-parser | xml-js | xml2json |
|---|---|---|---|
| Performance | π High speed | β‘ Moderate | π’ Lower |
| Validation | π Built-in validator | β Try-catch only | β Callback error |
| Structure | ποΈ Standard object | ποΈ Compact or Standard | ποΈ Fixed |
| Builder | π οΈ Included | π οΈ Included | β οΈ Limited |
| Maintenance | π Active | π Active | β οΈ Legacy |
fast-xml-parser is like a high-performance engine ποΈ β best for teams that need speed, validation, and modern features. Ideal for APIs, large data processing, and security-conscious apps.
xml-js is like a Swiss Army knife πͺ β perfect for teams that need flexibility in data structure and bidirectional conversion. Great for general-purpose integration tasks.
xml2json is like an older tool π§° β usable for quick scripts or legacy maintenance, but not recommended for new architecture due to limited updates.
Final Thought: For most modern frontend and Node.js projects, fast-xml-parser or xml-js will serve you better. They offer active support and features that match today's development standards.
Choose fast-xml-parser if performance is your top priority or if you need to validate XML against rules before parsing. It is ideal for high-traffic servers or large file processing where speed and safety matter most.
Choose xml-js if you need flexibility in how JSON is structured, such as switching between compact and standard modes. It is suitable for projects that require both XML-to-JSON and JSON-to-XML conversion with customizable options.
Choose xml2json only for legacy projects or simple scripts where external dependencies must be minimal. For new projects, avoid this package due to limited maintenance and consider xml-js or fast-xml-parser instead.
Validate XML, Parse XML to JS Object, or Build XML from JS Object without C/C++ based libraries and no callback.
It can handle big files (tested up to 100mb). XML Entities, HTML entities, and DOCTYPE entites are supported. Unpaired tags (Eg <br> in HTML), stop nodes (Eg <script> in HTML) are supported. It can also preserve Order of tags in JS object

Sponsor this project

This is a donation. No goods or services are expected in return. Any requests for refunds for those purposes will be rejected.
The list of users are mostly published by Github or communicated directly. Feel free to contact if you find any information wrong.
To use as package dependency
$ npm install fast-xml-parser
or
$ yarn add fast-xml-parser
To use as system command
$ npm install fast-xml-parser -g
To use it on a webpage include it from a CDN
Example
As CLI command
$ fxparser some.xml
In a node js project
const { XMLParser, XMLBuilder, XMLValidator} = require("fast-xml-parser");
const parser = new XMLParser();
let jObj = parser.parse(XMLdata);
const builder = new XMLBuilder();
const xmlContent = builder.build(jObj);
In a HTML page
<script src="path/to/fxp.min.js"></script>
:
<script>
const parser = new fxparser.XMLParser();
parser.parse(xmlContent);
</script>
Bundle size
| Bundle Name | Size |
|---|---|
| fxbuilder.min.js | 6.5K |
| fxparser.min.js | 20K |
| fxp.min.js | 26K |
| fxvalidator.min.js | 5.7K |
| v3 | v4 and v5 | v6 |
| documents |
note:
negative means error
* Y-axis: requests per second
Usage Trend of fast-xml-parser
This project exists thanks to all the people who contribute. [Contribute].
Thank you to all our backers! π [Become a backer]
