Fast XML Parser is a comprehensive XML processing library that validates XML data syntactically, parses XML to JavaScript objects, and builds XML from JavaScript objects without requiring C/C++ based dependencies. It offers high-performance XML processing with support for large files (tested up to 100MB), XML entities, HTML entities, DOCTYPE entities, unpaired tags, and stop nodes while maintaining the order of tags in JavaScript objects.
npm install fast-xml-parserimport { XMLParser, XMLValidator, XMLBuilder } from "fast-xml-parser";For CommonJS:
const { XMLParser, XMLValidator, XMLBuilder } = require("fast-xml-parser");import { XMLParser, XMLValidator, XMLBuilder } from "fast-xml-parser";
// Validate XML
const isValid = XMLValidator.validate(xmlString);
if (isValid !== true) {
console.log("Invalid XML:", isValid.err.msg);
}
// Parse XML to JavaScript object
const parser = new XMLParser();
const jsonObj = parser.parse(xmlString);
console.log(JSON.stringify(jsonObj, null, 2));
// Build XML from JavaScript object
const builder = new XMLBuilder();
const xmlOutput = builder.build(jsonObj);
console.log(xmlOutput);Fast XML Parser is built around three core components:
Fast validation of XML syntax and structure without the overhead of full parsing. Ideal for input validation and pre-processing checks.
XMLValidator.validate(xmlData: string, options?: validationOptions): true | ValidationError;
interface validationOptions {
allowBooleanAttributes?: boolean;
unpairedTags?: string[];
}
interface ValidationError {
err: {
code: string;
msg: string;
line: number;
col: number;
};
}Converts XML strings to JavaScript objects with comprehensive configuration options for handling attributes, namespaces, entities, and data types.
class XMLParser {
constructor(options?: X2jOptions);
parse(xmlData: string | Buffer, validationOptions?: validationOptions | boolean): any;
addEntity(entityIdentifier: string, entityValue: string): void;
static getMetaDataSymbol(): Symbol;
}
interface X2jOptions {
preserveOrder?: boolean;
attributeNamePrefix?: string;
attributesGroupName?: false | string;
textNodeName?: string;
ignoreAttributes?: boolean | (string | RegExp)[] | ((attrName: string, jPath: string) => boolean);
// ... extensive additional options
}Converts JavaScript objects to XML strings with formatting and structure control options. Supports both compact and pretty-printed output.
class XMLBuilder {
constructor(options?: XmlBuilderOptions);
build(jObj: any): string;
}
interface XmlBuilderOptions {
attributeNamePrefix?: string;
attributesGroupName?: false | string;
textNodeName?: string;
ignoreAttributes?: boolean | (string | RegExp)[] | ((attrName: string, jPath: string) => boolean);
format?: boolean;
indentBy?: string;
// ... additional formatting and processing options
}A built-in CLI is included but displays a deprecation warning and recommends using the standalone fxp-cli package instead.
# Binary command
fxparser [options] [input-file]
# Available options:
# -h, --help Show help information
# --version Show version number
# -ns Don't remove namespace prefixes
# -a Ignore attributes
# -c Don't parse tag and attribute values
# -o <file> Output to specified file
# -v Validate XML while parsing
# -V Validate XML only (no parsing)Usage Examples:
# Basic parsing from file
fxparser input.xml
# Parse from stdin
cat input.xml | fxparser
# With validation
fxparser -v input.xml
# Validate only
fxparser -V input.xml
# Output to file
fxparser -o output.json input.xml
# Parse without removing namespaces
fxparser -ns input.xml
# Ignore attributes
fxparser -a input.xml
# Don't convert values (keep as strings)
fxparser -c input.xmlDeprecation Notice: This CLI shows a deprecation warning. For new projects, install the dedicated CLI package:
npm install -g fxp-cli