# xmlbuilder2

> An XML builder for node.js

Latest version **4.0.3** (published 2025-12-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install xmlbuilder2
pnpm add xmlbuilder2
yarn add xmlbuilder2
bun add xmlbuilder2
```

## Health

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

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

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 4.0.3 |
| Published | 2025-12-01 |
| First published | 2019-11-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=20.0 |
| Dependencies | 4 |
| Unpacked size | 917.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 409 |
| Author | Ozgur Ozcitak |
| Maintainers | oozcitak, universalhandle |
| Keywords | xml, xmlbuilder |

## Links

- npm: https://www.npmjs.com/package/xmlbuilder2
- Repository: https://github.com/oozcitak/xmlbuilder2
- Issues: https://github.com/oozcitak/xmlbuilder2/issues
- npm.io page: https://npm.io/package/xmlbuilder2

## Dependencies (4)

- [js-yaml](https://npm.io/package/js-yaml.md) ^4.1.1
- [@oozcitak/dom](https://npm.io/package/@oozcitak/dom.md) ^2.0.2
- [@oozcitak/util](https://npm.io/package/@oozcitak/util.md) ^10.0.0
- [@oozcitak/infra](https://npm.io/package/@oozcitak/infra.md) ^2.0.2

## 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

- 4.0.3 (latest) — 2025-12-01
- 4.0.2 — 2025-12-01
- 4.0.1 — 2025-11-26
- 4.0.0 — 2025-10-16
- 3.1.1 — 2023-05-09
- 3.1.0 — 2023-04-24
- 3.0.2 — 2021-08-10
- 3.0.1 — 2021-07-29
- 3.0.0 — 2021-07-29
- 2.4.1 — 2021-04-08
- 2.4.0 — 2020-09-14
- 2.3.1 — 2020-08-05
- 2.3.0 — 2020-08-04
- 2.2.0 — 2020-07-14
- 2.1.7 — 2020-07-09
- … 35 more at https://npm.io/package/xmlbuilder2/versions

## README

# xmlbuilder2

An XML builder for [node.js](https://nodejs.org/).

![GitHub License](https://img.shields.io/github/license/oozcitak/xmlbuilder2)
![NPM Version](https://img.shields.io/npm/v/xmlbuilder2)
![NPM Downloads](https://img.shields.io/npm/dm/xmlbuilder2)
![jsDelivr hits (npm)](https://img.shields.io/jsdelivr/npm/hm/xmlbuilder2)

[![Node.js CI](https://github.com/oozcitak/xmlbuilder2/workflows/build/badge.svg)](https://github.com/oozcitak/xmlbuilder2/actions)
[![Code Coverage](https://codecov.io/gh/oozcitak/xmlbuilder2/branch/master/graph/badge.svg)](https://codecov.io/gh/oozcitak/xmlbuilder2)

[![GitHub Sponsors](https://img.shields.io/github/sponsors/oozcitak)](https://github.com/sponsors/oozcitak)

### Installation:

``` sh
npm install xmlbuilder2
```

### Documentation:

See: https://oozcitak.github.io/xmlbuilder2/


### Usage:

`xmlbuilder2` is a wrapper around DOM nodes which adds chainable functions to make it easier to create and work with XML documents. For example the following XML document:

``` xml
<?xml version="1.0"?>
<root att="val">
  <foo>
    <bar>foobar</bar>
  </foo>
  <baz/>
</root>
```

can be created with the following function chain:

``` js
const { create } = require('xmlbuilder2');

const root = create({ version: '1.0' })
  .ele('root', { att: 'val' })
    .ele('foo')
      .ele('bar').txt('foobar').up()
    .up()
    .ele('baz').up()
  .up();

// convert the XML tree to string
const xml = root.end({ prettyPrint: true });
console.log(xml);
```

___

The same XML document can be created by converting a JS object into XML nodes:

``` js
const { create } = require('xmlbuilder2');

const obj = {
  root: {
    '@att': 'val',
    foo: {
      bar: 'foobar'
    },
    baz: {}
  }
};

const doc = create(obj);
const xml = doc.end({ prettyPrint: true });
console.log(xml);
```
___

`xmlbuilder2` can also parse and serialize XML documents from different formats:
```js
const { create } = require('xmlbuilder2');

const xmlStr = '<root att="val"><foo><bar>foobar</bar></foo></root>';
const doc = create(xmlStr);

// append a 'baz' element to the root node of the document
doc.root().ele('baz');

const xml = doc.end({ prettyPrint: true });
console.log(xml);
```
which would output the same document string at the top of this page.

Or you could return a JS object by changing the `format` argument to `'object'`:
```js
const obj = doc.end({ format: 'object' });
console.log(obj);
```
```js
{
  root: {
    '@att': 'val',
    foo: {
      bar: 'foobar'
    },
    baz: {}
  }
}
```

___

You can convert between formats in one go with the `convert` function:

```js
const { convert } = require('xmlbuilder2');

const xmlStr = '<root att="val"><foo><bar>foobar</bar></foo></root>';
const obj = convert(xmlStr, { format: "object" });

console.log(obj);
```
```js
{
  root: {
    '@att': 'val',
    foo: {
      bar: 'foobar'
    }
  }
}
```

___

If you need to do some processing:

``` js
const { create } = require('xmlbuilder2');

const root = create().ele('squares');
root.com('f(x) = x^2');
for(let i = 1; i <= 5; i++)
{
  const item = root.ele('data');
  item.att('x', i);
  item.att('y', i * i);
}

const xml = root.end({ prettyPrint: true });
console.log(xml);
```

This will result in:

``` xml
<?xml version="1.0"?>
<squares>
  <!-- f(x) = x^2 -->
  <data x="1" y="1"/>
  <data x="2" y="4"/>
  <data x="3" y="9"/>
  <data x="4" y="16"/>
  <data x="5" y="25"/>
</squares>
```

### Usage in the browser:

You can build the minified production bundle (`lib/xmlbuilder2.min.js`) after cloning the repository and issuing `npx webpack` in your terminal. The bundle is also in the npm package, so you can also use a public npm CDN like [jsDelivr](https://www.jsdelivr.com/) or [unpkg](https://unpkg.com/):

```html
<!-- latest version from jsDelivr -->
<script src="https://cdn.jsdelivr.net/npm/xmlbuilder2/lib/xmlbuilder2.min.js"></script>
<!-- latest version from unpkg -->
<script src="https://unpkg.com/xmlbuilder2/lib/xmlbuilder2.min.js"></script>
```

### Donations:
Please consider becoming a sponsor to help support development.

[:heart: Github Sponsors](https://github.com/sponsors/oozcitak)

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