xml-formatter
Pretty-print and minify XML strings in JavaScript/TypeScript, respecting xml:space
Repository Health
Technical Analysis
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.
Used by 3 apps in this directory
Hoppscotch
Developer Tools
A lightweight, offline-capable API development ecosystem for testing HTTP, GraphQL, WebSocket, MQTT, and SSE endpoints across web, desktop, and CLI.
IT-Tools
Developer Tools
A unified collection of 88 web-based developer utilities — from JSON formatting to subnet calculation — all self-hostable, keyboard-searchable, and offline-ready.
Kibana
Analytics · Monitoring
Your open source window into the Elastic Stack — query, visualize, and act on data stored in Elasticsearch with real-time dashboards, AI-assisted search, and automated alerting.