# xml-formatter

> Converts a XML string into a human readable format (pretty print) while respecting the xml:space attribute

Latest version **3.7.0** (published 2026-03-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install xml-formatter
pnpm add xml-formatter
yarn add xml-formatter
bun add xml-formatter
```

## Health

**Score 60/100 (C)** — status: stable.

Positive: has types; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 3.7.0 |
| Published | 2026-03-13 |
| First published | 2016-11-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >= 16 |
| Dependencies | 1 |
| Unpacked size | 81.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 113 |
| Author | Chris Bottin |
| Maintainers | chrisbottin |
| Keywords | xml, pretty, print, pretty-print, indent, parser, formatter, xml:space, preserve |

## Links

- npm: https://www.npmjs.com/package/xml-formatter
- Repository: https://github.com/chrisbottin/xml-formatter
- Homepage: https://github.com/chrisbottin/xml-formatter#readme
- Issues: https://github.com/chrisbottin/xml-formatter/issues
- npm.io page: https://npm.io/package/xml-formatter

## Dependencies (1)

- [xml-parser-xo](https://npm.io/package/xml-parser-xo.md) ^4.1.5

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 3.7.0 (latest) — 2026-03-13
- 3.3.0-rc-minify.0 (rc-minify) — 2023-02-22
- 3.6.7 — 2025-09-18
- 3.6.6 — 2025-05-11
- 3.6.5 — 2025-03-21
- 3.6.4 — 2025-01-23
- 3.6.3 — 2024-07-07
- 3.6.2 — 2024-01-20
- 3.6.1 — 2024-01-18
- 3.6.0 — 2023-10-23
- 3.5.0 — 2023-08-05
- 3.4.1 — 2023-05-15
- 3.4.0 — 2023-05-15
- 3.3.2 — 2023-03-08
- 3.3.1 — 2023-03-07
- … 29 more at https://npm.io/package/xml-formatter/versions

## README

# xml-formatter

Converts XML into a human readable format (pretty print) while respecting the `xml:space` attribute.

Reciprocally, the `xml-formatter` package can minify pretty printed XML.

The `xml-formatter` package can also be used on the browser using the browserified version with a small footprint.

[![Build Status](https://github.com/chrisbottin/xml-formatter/actions/workflows/ci.yml/badge.svg)](https://github.com/chrisbottin/xml-formatter/actions/workflows/ci.yml) [![npm version](https://img.shields.io/npm/v/xml-formatter.svg)](https://npmjs.org/package/xml-formatter)

## Installation

```
$ npm install xml-formatter
```

## Example

### Usage:

```js
import xmlFormat from 'xml-formatter';

xmlFormat('<root><content><p xml:space="preserve">This is <b>some</b> content.</content></p>');
```

### Output:

```xml
<root>
    <content>
        <p xml:space="preserve">This is <b>some</b> content.</p>
    </content>
</root>
```

## Options

- `filter`: Function to filter out unwanted nodes by returning `false`.
  - type: `function(node) => boolean`
  - default: `() => true`
- `ignoredPaths`: List of XML element paths to ignore during formatting. 
This can be a partial path (element tag name) or full path starting from the document element e.g. `['/html/head/script', 'pre']`.
  - type: `string[]`
  - default: `[]`
- `indentation`: The value used for indentation.
  - type: `string`
  - default: `'    '`
- `collapseContent`: True to keep content in the same line as the element. Only works if element contains at least one text node.
  - type: `boolean`
  - default: `false`
- `lineSeparator`: Specify the line separator to use.
  - type: `string`
  - default: `\r\n`
- `whiteSpaceAtEndOfSelfclosingTag`: True to end self-closing tags with a space e.g. `<tag />`.
  - type: `boolean`
  - default: `false`
- `throwOnFailure`: Throw an error when XML fails to parse and get formatted otherwise the original XML is returned.
  - type: `boolean`
  - default: `true`
- `forceSelfClosingEmptyTag`: True to force empty tags to be self-closing.
  - type: `boolean`
  - default: `false`
- `attributeQuotes`: The quote characters that are used as attribute delimiters.
  - Valid options are 
    - 'single' to use single quotes 
    - 'double' to use double quotes
  - type: `'double|single'`
  - default: `'double'`

### Usage:
 
```js
import xmlFormat from 'xml-formatter';

xmlFormat('<root><!-- content --><content><p>This is <b>some</b> content.</content></p>', {
    indentation: '  ', 
    filter: (node) => node.type !== 'Comment', 
    collapseContent: true, 
    lineSeparator: '\n'
});

```

### Output:

```xml
<root>
  <content>
    <p>This is <b>some</b> content.</p>
  </content>
</root>
```

## Minify mode

### Usage:

```js
import xmlFormat from 'xml-formatter';

const xml = `
<root>
  <content>
    <p>
        This is <b>some</b> content.
    </p>
  </content>
</root>`;

xmlFormat.minify(xml, {
    filter: (node) => node.type !== 'Comment',
    collapseContent: true
});

```

### Output:

```xml
<root><content><p>This is<b>some</b>content.</p></content></root>
```

## On The Browser

The code is transpiled using [Babel](https://babeljs.io/) with [@babel/preset-env](https://babeljs.io/docs/en/babel-preset-env) default values and bundled using [browserify](https://browserify.org/).

### Using `require('xml-formatter')`

### Page:
 
```html
<script type="text/javascript" src="dist/browser/xml-formatter.js"></script>
```

### Usage:
 
```js
const xmlFormatter = require('xml-formatter');

xmlFormat('<root><content><p xml:space="preserve">This is <b>some</b> content.</content></p>');
```

### Using global function `xmlFormatter`

### Page:

```html
<script type="text/javascript" src="dist/browser/xml-formatter-singleton.js"></script>
```

### Usage:

```js
xmlFormatter('<root><content><p xml:space="preserve">This is <b>some</b> content.</content></p>');
```

### Output

```xml
<root>
    <content>
        <p xml:space="preserve">This is <b>some</b> content.</p>
    </content>
</root>
```

## License

MIT

---
_Source: https://npm.io/package/xml-formatter · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
