Use fast-xml-parser in the browser
master<script> tag using a CDN provider such as cdnjs.repository·master·Indexed 25 days ago
https://github.com/naturalintelligence/fast-xml-parserA high-performance, pure JavaScript library for validating, parsing, and building XML. It supports CommonJS, ESM, and browser environments, and can handle files up to 100MB. The library provides the XMLParser class for converting XML to JS objects, XMLBuilder for converting JS objects to XML, and XMLValidator for syntactic validation. It includes extensive configuration options for attribute handling, CDATA, comments, and number parsing.
<script> tag using a CDN provider such as cdnjs.To customize XML parsing, instantiate the XMLParser class with an options object. By default, attributes are ignored unless ignoreAttributes: false is specified.
const {XMLParser} = require('fast-xml-parser');
const options = {
ignoreAttributes : false
};
const parser = new XMLParser(options);
let jsonObj = parser.parse(xmlDataStr);You can install fast-xml-parser as a project dependency using npm or yarn, or install it globally to use it as a system command.
To use it in a Node.js project:
$ npm install fast-xml-parser
# or
$ yarn add fast-xml-parserTo use it as a system command:
$ npm install fast-xml-parser -gTo use it on a webpage, include it via a CDN.
$ npm install fast-xml-parserFast XML Parser supports Processing Instructions (PI tags), but treats them as normal tags during parsing. To ensure attributes within PI tags are processed correctly, you must set ignoreAttributes: false and allowBooleanAttributes: true in your parser options.
Note the following behavior:
? character.#text property is always empty to maintain consistency with other parsed properties.const options = {
ignoreAttributes: false,
format: true,
preserveOrder: true,
allowBooleanAttributes: true
};
const parser = new XMLParser(options);
let result = parser.parse(xmlData);To use the library in an HTML page, include the minified script from a CDN and access the classes via the fxparser global object.
<script src="path/to/fxp.min.js"></script>
<script>
const parser = new fxparser.XMLParser();
parser.parse(xmlContent);
</script>To reconstruct XML containing Processing Instructions from a parsed JS ordered object, use XMLBuilder with the following configuration:
ignoreAttributes: falsepreserveOrder: trueallowBooleanAttributes: truesuppressBooleanAttributes: true (optional, used to clean up boolean attribute output)This ensures that PI tags (starting with ?) and their attributes are correctly formatted in the output XML.
const options = {
ignoreAttributes: false,
preserveOrder: true,
allowBooleanAttributes: true,
suppressBooleanAttributes: true
};
const builder = new XMLBuilder(options);
const output = builder.build(result);You can run the benchmarks using the following commands:
To convert a JS object back into an HTML document (round-tripping), you must use XMLBuilder with preserveOrder: true enabled in both the XMLParser and XMLBuilder configurations. This ensures the structure and order of elements are maintained.
Required configuration for round-tripping:
XMLParser: Set preserveOrder: true.XMLBuilder: Set preserveOrder: true, format: true, and suppressEmptyNode: true.const parsingOptions = {
ignoreAttributes: false,
preserveOrder: true,
unpairedTags: ["hr", "br", "link", "meta"],
stopNodes : [ "*.pre", "*.script"],
processEntities: true,
htmlEntities: true
};
const parser = new XMLParser(parsingOptions);
let result = parser.parse(html);
const builderOptions = {
ignoreAttributes: false,
format: true,
preserveOrder: true,
suppressEmptyNode: true,
unpairedTags: ["hr", "br", "link", "meta"],
stopNodes : [ "*.pre", "*.script"],
};
const builder = new XMLBuilder(builderOptions);
const output = builder.build(result);When using jPath: false and Expression objects in callbacks, always pre-compile your expressions outside of the callback. Creating a new Expression() inside a callback is extremely slow as it parses the pattern for every single node processed.
// ✅ GOOD - Parse once, reuse many times
const expr = new Expression("..user[id]");
const parser = new XMLParser({
stopNodes: [expr],
jPath: false,
tagValueProcessor: (tagName, val, matcher) => {
if (matcher.matches(expr)) {
// Fast matching - expression already parsed
}
return val;
}
});
// ❌ BAD - Parse on every callback
const parser = new XMLParser({
jPath: false,
tagValueProcessor: (tagName, val, matcher) => {
// Slow - creates new Expression every time
if (matcher.matches(new Expression("..user[id]"))) {
// ...
}
return val;
}
});$ npm iThe XMLBuilder functionality has been moved from the fast-xml-parser package to a dedicated standalone package called fast-xml-builder. To avoid bugs affecting the parser and to ensure future compatibility, you should migrate your imports. XMLBuilder will be removed from the fast-xml-parser package in the next major version.
// From
import { XMLBuilder } from "fast-xml-parser";
// To
import XMLBuilder from "fast-xml-builder";Starting from v5.5.0, stopNodes supports powerful pattern matching via path-expression-matcher. You can use strings for simple patterns or Expression objects for complex logic including exact paths, deep wildcards, attribute conditions, position selectors, and namespaces.
Note on Wildcards:
"*.script" is automatically converted to "..script" (matches at any depth) for backward compatibility.new Expression("*.script") matches only at one level. To match at any depth with an Expression object, use "..script" instead.import { Expression } from 'path-expression-matcher';
const parser = new XMLParser({
stopNodes: [
"..script", // Deep wildcard - script anywhere
"..style", // Deep wildcard - style anywhere
new Expression("html.body.script"), // Exact path
new Expression("..pre"), // Any pre tag
new Expression("div[class=code]"), // With attribute condition
new Expression("item:first"), // Position selector
new Expression("ns::tag") // Namespace support
]
});