# xml

> Fast and simple xml generator. Supports attributes, CDATA, etc. Includes tests and examples.

Latest version **1.0.1** (published 2016-01-31) · MIT license · 0 weekly downloads

## Install

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

## Health

**Score 23/100 (F)** — status: abandoned.

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2016-01-31 |
| First published | 2011-04-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/xml) |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 282 |
| Author | Dylan Greene |
| Maintainers | dylang, erisds |
| Keywords | xml, create, builder, json, simple |

## Links

- npm: https://www.npmjs.com/package/xml
- Repository: https://github.com/dylang/node-xml
- Homepage: http://github.com/dylang/node-xml
- Issues: http://github.com/dylang/node-xml/issues
- npm.io page: https://npm.io/package/xml

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.0.1 (latest) — 2016-01-31
- 1.0.0 — 2014-09-26
- 0.0.12 — 2014-01-28
- 0.0.10 — 2013-11-08
- 0.0.9 — 2013-11-08
- 0.0.8 — 2013-09-15
- 0.0.7 — 2011-10-28
- 0.0.5 — 2011-05-08
- 0.0.4 — 2011-04-13
- 0.0.3 — 2011-04-11
- 0.0.2 — 2011-04-11

## README

# xml [![Build Status](https://api.travis-ci.org/dylang/node-xml.svg)](http://travis-ci.org/dylang/node-xml)

[![NPM](https://nodei.co/npm/xml.png?downloads=true)](https://nodei.co/npm/xml/)

> Fast and simple Javascript-based XML generator/builder for Node projects.

## Install

   $ npm install xml

## API

### `xml(xmlObject, options)`

Returns a `XML` string.

```js
var xml = require('xml');
var xmlString = xml(xmlObject, options);
```

#### `xmlObject`

`xmlObject` is a normal JavaScript Object/JSON object that defines the data for the XML string.

Keys will become tag names.
Values can be an `array of xmlObjects` or a value such as a `string` or `number`.

```js
xml({a: 1}) === '<a>1</a>'
xml({nested: [{ keys: [{ fun: 'hi' }]}]}) === '<nested><keys><fun>hi</fun></keys></nested>'
```

There are two special keys:

`_attr`

Set attributes using a hash of key/value pairs.

```js
xml({a: [{ _attr: { attributes: 'are fun', too: '!' }}, 1]}) === '<a attributes="are fun" too="!">1</a>'
````
`_cdata`

Value of `_cdata` is wrapped in xml `![CDATA[]]` so the data does not need to be escaped.

```js
xml({a: { _cdata: "i'm not escaped: <xml>!"}}) === '<a><![CDATA[i\'m not escaped: <xml>!]]></a>'
```

Mixed together:
```js
xml({a: { _attr: { attr:'hi'}, _cdata: "I'm not escaped" }}) === '<a attr="hi"><![CDATA[I\'m not escaped]]></a>'
```

#### `options`

`indent` _optional_ **string** What to use as a tab. Defaults to no tabs (compressed).
 For example you can use `'\t'` for tab character, or `'  '` for two-space tabs.

`stream` Return the result as a `stream`.

**Stream Example**

```js
var elem = xml.element({ _attr: { decade: '80s', locale: 'US'} });
var stream = xml({ toys: elem }, { stream: true });
stream.on('data', function (chunk) {console.log("data:", chunk)});
elem.push({ toy: 'Transformers' });
elem.push({ toy: 'GI Joe' });
elem.push({ toy: [{name:'He-man'}] });
elem.close();

/*
result:
data: <toys decade="80s" locale="US">
data:     <toy>Transformers</toy>
data:     <toy>GI Joe</toy>
data:     <toy>
            <name>He-man</name>
          </toy>
data: </toys>
*/
```

`Declaration` _optional_ Add default xml declaration as first node.

_options_ are:
* encoding: 'value'
* standalone: 'value'
          
**Declaration Example**

```js
xml([ { a: 'test' }], { declaration: true })
//result: '<?xml version="1.0" encoding="UTF-8"?><a>test</a>'

xml([ { a: 'test' }], { declaration: { standalone: 'yes', encoding: 'UTF-16' }})
//result: '<?xml version="1.0" encoding="UTF-16" standalone="yes"?><a>test</a>'
```

## Examples

**Simple Example**

```js
var example1 = [ { url: 'http://www.google.com/search?aq=f&sourceid=chrome&ie=UTF-8&q=opower' } ];
console.log(XML(example1));
//<url>http://www.google.com/search?aq=f&amp;sourceid=chrome&amp;ie=UTF-8&amp;q=opower</url>
```

**Example with attributes**

```js
var example2 = [ { url: { _attr: { hostname: 'www.google.com', path: '/search?aq=f&sourceid=chrome&ie=UTF-8&q=opower' }  } } ];
console.log(XML(example2));
//result: <url hostname="www.google.com" path="/search?aq=f&amp;sourceid=chrome&amp;ie=UTF-8&amp;q=opower"/>
```

**Example with array of same-named elements and nice formatting**

```js
var example3 = [ { toys: [ { toy: 'Transformers' } , { toy: 'GI Joe' }, { toy: 'He-man' } ] } ];
console.log(XML(example3));
//result: <toys><toy>Transformers</toy><toy>GI Joe</toy><toy>He-man</toy></toys>
console.log(XML(example3, true));
/*
result:
<toys>
    <toy>Transformers</toy>
    <toy>GI Joe</toy>
    <toy>He-man</toy>
</toys>
*/
```

**More complex example**

```js
var example4 = [ { toys: [ { _attr: { decade: '80s', locale: 'US'} }, { toy: 'Transformers' } , { toy: 'GI Joe' }, { toy: 'He-man' } ] } ];
console.log(XML(example4, true));
/*
result:
<toys decade="80s" locale="US">
    <toy>Transformers</toy>
    <toy>GI Joe</toy>
    <toy>He-man</toy>
</toys>
*/
```

**Even more complex example**

```js
var example5 = [ { toys: [ { _attr: { decade: '80s', locale: 'US'} }, { toy: 'Transformers' } , { toy: [ { _attr: { knowing: 'half the battle' } }, 'GI Joe'] }, { toy: [ { name: 'He-man' }, { description: { _cdata: '<strong>Master of the Universe!</strong>'} } ] } ] } ];
console.log(XML(example5, true));
/*
result:
<toys decade="80s" locale="US">
    <toy>Transformers</toy>
    <toy knowing="half the battle">
        GI Joe
    </toy>
    <toy>
        <name>He-man</name>
        <description><![CDATA[<strong>Master of the Universe!</strong>]]></description>
    </toy>
</toys>
*/
```

## Tests

Tests included use [AVA](https://ava.li). Use `npm test` to run the tests.

    $ npm test

## Examples

There are examples in the examples directory.

# Contributing

Contributions to the project are welcome. Feel free to fork and improve. I accept pull requests when tests are included.

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