API Reference

fast-xml-parser

Fast XML to JSON parser and builder with validation, custom attributes and namespace handling.

Official Documentation ↗
CompatibilityThis documentation covers version v5.12.0. Check the official docs for the latest API surface.

Installation

npmnpm install fast-xml-parser
pnpmpnpm add fast-xml-parser
yarnyarn add fast-xml-parser

Methods

Overview

fast-xml-parser converts between XML and JavaScript objects, with options for attributes, namespaces, text nodes and validation. It is optimised for speed on large documents.

Core Methods

XMLParser parses strings into objects, XMLBuilder serializes objects back to XML, and XMLValidator checks well-formedness. Option flags control attributes, text nodes, arrays and prefixes.

Usage Notes

Validate untrusted XML before parsing, normalize arrays for repeated elements, and configure attribute prefixes to match your object model. Builder options should mirror parser options for round-trips.

Common Pitfalls

Without array normalization, consumers face inconsistent types for single versus multiple children. Externally sourced XML can contain entity expansion attacks, so disable external entities and cap input size.

FAQ

How are attributes represented?
By default attributes are prefixed with an attributeNamePrefix, often @_. Set ignoreAttributes to false to keep them, or provide custom prefixing options.
How do I validate XML?
The XMLValidator class checks well-formedness and returns an error object with line and column, which is useful before parsing untrusted input.
Why does my root element shape change?
A single child differs from an array of children in the produced object. Use isArray callbacks or alwaysArray options to normalize repeated elements into arrays consistently.