These libraries handle XML data in JavaScript, but they serve different roles. Some focus on parsing XML into JSON objects, others build XML from scratch, and some provide a DOM interface similar to browsers. fast-xml-parser and xml2js convert XML to JSON for easy manipulation. xmlbuilder specializes in generating XML structures. xmldom offers a DOM API for XML. libxmljs and libxmljs2 use native bindings for high performance but require Node.js. Choosing the right tool depends on whether you need speed, browser support, or DOM compatibility.
Working with XML in JavaScript requires choosing between speed, compatibility, and API style. The six packages here cover three main areas: parsing XML into data, building XML from data, and manipulating XML via DOM methods. Some rely on native code for performance, while others run purely in JavaScript. Let's compare how they handle real-world tasks.
The core difference lies in how these libraries process XML. Native bindings offer speed but limit where you can run your code. Pure JavaScript works everywhere but may be slower on massive files.
libxmljs uses native C++ bindings to libxml2.
// libxmljs: Native parsing
const libxmljs = require("libxmljs");
const xmlDoc = libxmljs.parseXmlString('<root><item>1</item></root>');
console.log(xmlDoc.toString());
libxmljs2 is a maintained fork of libxmljs.
// libxmljs2: Native parsing (fork)
const libxmljs2 = require("libxmljs2");
const xmlDoc = libxmljs2.parseXmlString('<root><item>1</item></root>');
console.log(xmlDoc.toString());
fast-xml-parser is written entirely in JavaScript.
// fast-xml-parser: Pure JS parsing
const { XMLParser } = require("fast-xml-parser");
const parser = new XMLParser();
const result = parser.parse('<root><item>1</item></root>');
console.log(result);
xml2js is also pure JavaScript.
// xml2js: Pure JS parsing
const { parseStringPromise } = require("xml2js");
const result = await parseStringPromise('<root><item>1</item></root>');
console.log(result);
xmldom provides a DOM parser in pure JavaScript.
DOMParser.@xmldom/xmldom.// xmldom: DOM parsing
const { DOMParser } = require("xmldom");
const doc = new DOMParser().parseFromString('<root><item>1</item></root>', 'text/xml');
console.log(doc.toString());
How you access data matters. JSON is easier for JavaScript logic. DOM trees allow XPath and node traversal.
fast-xml-parser converts XML directly to JSON objects.
// fast-xml-parser: JSON output
const parser = new XMLParser();
const json = parser.parse('<user id="1">Alice</user>');
// { user: { "@_id": "1", "#text": "Alice" } }
xml2js also outputs JSON but with a different structure.
// xml2js: JSON output
const result = await parseStringPromise('<user id="1">Alice</user>');
// { user: { $: { id: "1" }, _: "Alice" } }
xmldom returns a DOM tree.
getElementsByTagName to find nodes.// xmldom: DOM output
const doc = new DOMParser().parseFromString('<user id="1">Alice</user>', 'text/xml');
const node = doc.getElementsByTagName('user')[0];
console.log(node.getAttribute('id'));
libxmljs and libxmljs2 return native DOM-like objects.
// libxmljs2: XPath query
const xmlDoc = libxmljs2.parseXmlString('<root><item id="1"/></root>');
const nodes = xmlDoc.findall('//item[@id]');
console.log(nodes.length);
Some projects need to create XML, not just read it. Dedicated builders simplify this task.
xmlbuilder is designed specifically for creating XML.
// xmlbuilder: Creating XML
const builder = require("xmlbuilder");
const xml = builder.create('root')
.ele('item', { id: "1" }).txt('Value').up()
.end();
fast-xml-parser includes a builder component.
// fast-xml-parser: Building from JSON
const { XMLBuilder } = require("fast-xml-parser");
const builder = new XMLBuilder();
const xml = builder.build({ root: { item: "Value" } });
xml2js also has a Builder class.
xml2js for parsing.// xml2js: Building from JSON
const { Builder } = require("xml2js");
const builder = new Builder();
const xml = builder.buildObject({ root: { item: "Value" } });
Security and active maintenance are critical for XML libraries due to vulnerabilities like XXE.
xmldom is officially deprecated.
@xmldom/xmldom.// xmldom: Deprecation warning
// Do not use in new projects. Switch to @xmldom/xmldom.
libxmljs has had periods of low activity.
libxmljs2 is the recommended fork for stability.// libxmljs vs libxmljs2
// Prefer libxmljs2 for ongoing Node.js compatibility.
fast-xml-parser and xml2js are actively maintained.
// fast-xml-parser & xml2js
// Both are safe choices for long-term projects.
| Package | Environment | Output Type | Building Support | Maintenance |
|---|---|---|---|---|
fast-xml-parser | Any (Pure JS) | JSON | ✅ Yes | ✅ Active |
libxmljs | Node (Native) | DOM/XPath | ❌ No | ⚠️ Stalled |
libxmljs2 | Node (Native) | DOM/XPath | ❌ No | ✅ Active Fork |
xml2js | Any (Pure JS) | JSON | ✅ Yes | ✅ Active |
xmlbuilder | Any (Pure JS) | XML String | ✅ Yes (Only) | ✅ Active |
xmldom | Any (Pure JS) | DOM | ❌ No | ❌ Deprecated |
fast-xml-parser is the best all-rounder for modern apps. It handles parsing and building with high speed and no native dependencies. Use it for APIs, configuration files, and data exchange.
xml2js is a solid alternative if you need legacy compatibility. It is stable and well-understood, though slightly slower than fast-xml-parser.
libxmljs2 is the choice for heavy server-side workloads. Use it if you need XPath or native performance and can guarantee a Node.js environment.
xmlbuilder shines when generating complex XML documents. Pair it with a parser if you need round-trip support.
xmldom should be avoided. Switch to @xmldom/xmldom if you strictly need DOM methods in JavaScript.
Final Thought: For most frontend and full-stack developers, pure JavaScript solutions like fast-xml-parser offer the best balance of safety, speed, and deployment flexibility.
Choose fast-xml-parser when you need high performance in a pure JavaScript environment. It handles both parsing and building without native dependencies, making it safe for browsers and serverless functions. It supports validation and complex XML features like attributes and tags. This is ideal for modern APIs where speed and security matter.
Choose libxmljs only if you are locked into a Node.js environment and require native libxml2 features. Be aware that it requires compilation and may fail on systems without build tools. It is not suitable for frontend or serverless deployments. Consider libxmljs2 instead for better maintenance support.
Choose libxmljs2 if you need native XML processing in Node.js but want a more actively maintained fork. It offers the same performance benefits as libxmljs with fewer stagnation risks. Like its predecessor, it cannot run in browsers due to native bindings. Use this for heavy-duty server-side XML workflows.
Choose xml2js for stable, proven XML-to-JSON conversion in Node.js or bundled web apps. It has been around for years and handles most standard XML structures reliably. The API is callback-based but supports promises. It is a safe default for legacy systems or projects prioritizing stability over raw speed.
Choose xmlbuilder when your primary task is generating XML documents from JavaScript objects. It provides a clean chainable API for constructing nodes and attributes. It does not focus on parsing, so pair it with a parser if you need bidirectional support. This is best for creating feeds, sitemaps, or SOAP requests.
Avoid xmldom for new projects as it is deprecated in favor of @xmldom/xmldom. It mimics the browser DOM API for XML, which is useful for XPath or DOM manipulation. If you must use it, know that security patches may be delayed. Only select this if you strictly require DOM methods in a Node environment.
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
Flexible-XML-Parser is 2 times faster than this library and allows to deal with incomplete XML/HTML. Output is highly customizable. Build whatever you want. So if you're fine with some extra configuration then try it out.
Please join Discord community for pre release announcements and discussions. This will prevent us to release breaking changes.
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]
