# xml-mapping

> provide a bidirectionnal mapping between XML and JS data Structure (aka JSON)

Latest version **1.7.2** (published 2022-01-31) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.7.2 |
| Published | 2022-01-31 |
| First published | 2012-02-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=0.2.0 |
| Dependencies | 2 |
| Unpacked size | 50 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 40 |
| Author | Nicolas Thouvenin |
| Maintainers | touv, niederlec |
| Keywords | xml, json, json2xml, xml2json |

## Links

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

## Dependencies (2)

- [sax](https://npm.io/package/sax.md) =0.4.3
- [xml-writer](https://npm.io/package/xml-writer.md) >=1.7.0

## 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.7.2 (latest) — 2022-01-31
- 1.7.1 — 2016-02-08
- 1.7.0 — 2014-08-29
- 1.6.1 — 2014-07-09
- 1.5.2 — 2013-12-11
- 1.5.1 — 2013-11-12
- 1.5.0 — 2013-09-13
- 1.4.1 — 2013-07-19
- 1.4.0 — 2013-07-19
- 1.3.1 — 2013-07-12
- 1.2.1 — 2013-05-06
- 1.2.0 — 2013-02-28
- 1.1.2 — 2013-02-15
- 1.1.1 — 2013-02-15
- 1.1.0 — 2012-12-19
- … 4 more at https://npm.io/package/xml-mapping/versions

## README

# xml2json and json2xml for NodeJS

[![Build Status](https://secure.travis-ci.org/Inist-CNRS/node-xml-mapping.png?branch=master)](http://travis-ci.org/Inist-CNRS/node-xml-mapping)

It's native javascript implementation of a bidirectional converter between XML and JS data structure (aka JSON).
You can convert any type of XML documents in an Javascript data structure.
You can also do the reverse, converting a Javascript data structure in XML String. XML is still valid.

## Contributors

  * [Nicolas Thouvenin](https://github.com/touv)
  * [Joe Ibershoff](https://github.com/zacronos)
  * [Yura Zenevich](https://github.com/yzen)
  * [Thorsten Lorenz](https://github.com/thlorenz)
  * [ntgn81](https://github.com/ntgn81)
  * [Rowell Cruz](https://github.com/rcruz)
  * [JT](https://github.com/spikyjt)
  * [Bindu Wavell](https://github.com/binduwavell)
  * [niederlec](https://github.com/niederlec)

# Installation

With [npm](http://npmjs.org) do:

    $ npm install xml-mapping


# Usage
```javascript
var XMLMapping = require('xml-mapping');

var json = XMLMapping.load('<key>value</key>');
var xml  = XMLMapping.dump(json);

console.log(xml,json);
console.log(json);
```

Output:

    <key>value</key> { key: { '$t': 'value' } }

# Convention

The rules for converting XML to JSON are those used by Google in its GData protocol. More information here : http://code.google.com/apis/gdata/docs/json.html

# Tests

Use [nodeunit](https://github.com/caolan/nodeunit) to run the tests.

    $ npm install nodeunit
    $ nodeunit test

# API Documentation

## load(String xml, Object options)
Transform a string with XML in Javascript data structure (JSON).
**Return Object.**

###Options

**Warning : options break the symmetry. This means that the JSON generated may not reproduce the same XML**

* `throwErrors` - *boolean* - Flag to throw errors of the SAX Parser ; *default : false*
* `nested` - *boolean* - Flag to ignore nested tags inside text : *default : false*
* `specialChar` - *string* - Set the first character of XML tag ($t, $text, $cd, $cdata, $e, $element, $c, $comment); *default : $*
* `longTag` - *boolean* - Use long names tags($text, $element, $cdata, $comment) rather than short names ($t, $cd, $e, $c); *default : false*
* `comments` - *boolean* - Flag to ignore comments, if false all the comments will be ignored : *default : true*
* `parserInfos` - *boolean* - Flag to add some attributes generated by the parser (order, line, column, name), if true informations are inserted in the attributes list of all tags : *default : false*

```javascript
var xml = '<title>Title is <strong>important</strong></title>';
var json = XMLMapping.load(xml, { nested: true });
console.log(json);

// Should output:
// { title : { $t : 'Title is<strong>important</strong>', strong: { '$t': 'important' }} }
```

* `arrays` - *array* - an array of basic XPath strings that specify XML nodes that should be array, even when there is only one such node.

```javascript
var xml = '<key1>value1</key1><key2><key3>value3</key3></key2>';
var json = XMLMapping.load(xml, {
 arrays: [
  '/key1',
  '/key2/key3'
 ]
});
console.log(json);

// Should output:
// (Note that value of key1 and key3 are arrays, as specified in options)
// {"key1":[{"$t":"value1"}],"key2":{"key3":[{"$t":"value3"}]}}
```


## dump(Object json, Object options)
Transform a Javascript data structure (JSON) in XML string. **Return String.**

###Options

**Warning : options break the symmetry. This means that the XML generated may not reproduce the same JSON**

* `indent` - *boolean* - Flag to throw errors of the SAX Parser ; *default : false*
* `header` - *boolean* - Flag to add XML header; *default : false*
* `version` - *string* - Set version attribute of XML header (see header flag); *default : 1.0*
* `encoding` - *string* - Set encoding attribute of XML header (see header flag); *default : UTF-8*
* `specialChar` - *string* - Set the first character of XML tag ($t, $text, $cd, $cdata, $e, $element, $c, $comment); *default : $*


## tojson(String xml)
Alias of load.

## toxml(Object json)
Alias of dump.

# Also

* https://github.com/estheban/node-json2xml
* https://github.com/buglabs/node-xml2json

# License

[MIT/X11](https://raw.github.com/touv/node-xml-mapping/master/LICENSE)


[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/touv/node-xml-mapping/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

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