xml-formatter

Pretty-print and minify XML strings in JavaScript/TypeScript, respecting xml:space

Library
npm
v3.7.0
113stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
69/100Good
Development Activity88
Maintenance48
Community68
Maturity60
Momentum12

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
70/100Good
Architecture68
Code Quality72
Innovation58
Learning Curve82

xml-formatter converts XML strings into human-readable, indented output, correctly respecting the xml:space="preserve" attribute so whitespace-sensitive content (like inline text with mixed markup) isn’t mangled during formatting — something naive regex-based XML prettifiers commonly get wrong. It can also run in reverse to minify already-formatted XML back down to a compact string.

The library works in both Node.js and browsers (via a browserified build), and supports configurable indentation, per-path node filtering/ignoring, and content-collapsing options, making it useful for XML config viewers, diff tools, or any pipeline that needs to display or normalize XML for humans.

What You Get

  • A single xmlFormat() function to pretty-print XML with configurable indentation
  • Correct handling of xml:space="preserve" so mixed text/element content keeps its intended whitespace
  • A minify mode (xmlFormat.minify()) to collapse formatted XML back to a compact string
  • Node filtering (filter) and path-based ignoring (ignoredPaths) to skip or preserve specific elements during formatting
  • Works in Node.js and in browsers via a pre-built browserified bundle

Common Use Cases

  • Pretty-printing XML API responses or config files for display in developer tools or logs
  • Normalizing XML before diffing two documents so formatting differences don’t create false positives
  • Minifying human-edited XML config files before storage or transmission to reduce payload size
  • Building XML viewers or editors that need readable, correctly indented output while preserving significant whitespace in content

Under The Hood

Architecture The entire implementation lives in a single src/index.ts (~290 lines) that first parses the input XML into a lightweight node tree (leveraging a bundled parser), then walks that tree recursively to emit indented output, tracking an xml:space context flag as it descends so preserved-whitespace subtrees are emitted verbatim rather than re-indented. Tech Stack Written in TypeScript with zero runtime dependencies beyond its internal parser, compiled to three separate targets — CommonJS, ESM, and a Browserify bundle — via distinct tsc/browserify build scripts, reflecting an explicit goal of working identically in Node.js, ESM bundlers, and directly in browsers via a <script> tag. Code Quality Tests in test/ run via mocha with ts-node, covering both a Node test suite and a dedicated browser test runner (test/browser/run-browser-tests.ts) to verify the browserified build behaves identically to the Node build; the project uses renovate.json for automated dependency updates and ESLint for linting. API Design The primary API is a single function call — xmlFormat(xmlString, options) — with an options object covering indentation, filter, ignoredPaths, and collapseContent, keeping the common case (just prettify this XML) to one line while still exposing enough hooks for selective node handling.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search