# jxon

> A complete, bidirectional, JXON library

Latest version **2.0.0-beta.5** (published 2017-05-02) · GPL v3 license · 0 weekly downloads

## Install

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

## 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 | 2.0.0-beta.5 |
| Published | 2017-05-02 |
| First published | 2014-01-09 |
| Weekly downloads | 0 |
| License | GPL v3 |
| TypeScript types | separate (@types/jxon) |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 (+16 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 87 |
| Author | MDN |
| Maintainers | igord, tyr_asd |
| Keywords | jxon, bidirectional, loseless, badgerfish, parker convention, xml to js, xml2js, xml to json, xml2json, js to xml, js2xml, json to xml, json2xml |

## Links

- npm: https://www.npmjs.com/package/jxon
- Repository: https://github.com/tyrasd/jxon
- Homepage: https://github.com/tyrasd/jxon#readme
- Issues: https://github.com/tyrasd/jxon/issues
- npm.io page: https://npm.io/package/jxon

## Dependencies (1)

- [xmldom](https://npm.io/package/xmldom.md) ^0.1.21

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

- 2.0.0-beta.5 (latest) — 2017-05-02
- 2.0.0-beta.4 — 2016-11-22
- 2.0.0-beta.3 — 2016-08-14
- 2.0.0-beta.2 — 2016-04-22
- 2.0.0-beta.1 — 2016-04-22
- 1.6.1 — 2016-01-12
- 1.6.0 — 2016-01-04
- 1.5.6 — 2015-12-29
- 1.5.5 — 2015-11-25
- 1.5.4 — 2015-08-13
- 1.5.3 — 2015-08-13
- 1.5.2 — 2015-08-02
- 1.5.1 — 2015-07-25
- 1.5.0 — 2015-07-22
- 1.4.0 — 2015-06-30
- … 7 more at https://npm.io/package/jxon/versions

## README

JXON [![Build Status](https://secure.travis-ci.org/tyrasd/jxon.svg)](https://travis-ci.org/tyrasd/jxon)
====

A complete, bidirectional, JXON (lossless JavaScript XML Object Notation) library. Packed as UMD.

Implementation of Mozilla's [JXON](https://developer.mozilla.org/en-US/docs/JXON) code. Head over to MDN for [Documentation](https://developer.mozilla.org/en-US/docs/JXON#Usage).


#### Example:

```js
{name: 'myportal'} 
<name>myportal</name>

{user: {
    username: 'testUser1',
    password: 'yolo',
    enabled: true
}}
<user>
  <username>testUser1</username>
  <password>yolo</password>
  <enabled>true</enabled>
</user>

{tag: {
    $type: 'regular'
    $blacklist: false
    _: 'Backbase'
}}
<tag type="regular" blacklist="false">Backbase</tag>

{dogs: {
    name: ['Charlie', {$nick: 'yes', _:'Mad Max'}]
}}
<dogs>
    <name>Charlie</name>
    <name nick="yes">Mad Max</name>
</dogs>
```

# API

### .config(cnf)
**Overrides default configuration properties**
- cnf - Object with configuration properties.

_Defaults:_

```js
valueKey: '_',
attrKey: '$',
attrPrefix: '$',
lowerCaseTags: false,
trueIsEmpty: false,
autoDate: false,
ignorePrefixedNodes: false,
parseValues: false,
parserErrorHandler: undefined
```

### .stringToJs(xmlString)
**Converts XML string to JS object.**
- xmlString - XML string to convert to JXON notation JS object

### .jsToString(obj)
**Converts JS object to XML string.**
- obj - JS object in JXON notation to convert to XML string

### .xmlToJs(xmlDocument, verbosity, freeze, nestedAttributes)
**Converts XML document to JS object. _Alias: JXON.build_**
- xmlDocument - The XML document to be converted into JavaScript Object.
- verbosity - Optional verbosity level of conversion, from 0 to 3. It is almost equivalent to our algorithms from #4 to #1 (default value is 1, which is equivalent to the algorithm #3).
- freeze - Optional boolean expressing whether the created object must be freezed or not (default value is false).
- nestedAttributes - Optional boolean expressing whether the the nodeAttributes must be nested into a child-object named keyAttributes or not (default value is false for verbosity levels from 0 to 2; true for verbosity level 3).
 
Example:
```js
var myObject = JXON.build(xmlDoc);
```
### .jsToXml(obj, namespaceURI, qualifiedNameStr, documentType)
**Converts JS object to XML document. _Alias: JXON.unbuild_**
- obj - The JavaScript Object from which you want to create your XML Document.
- namespaceURI - Optional DOMString containing the namespace URI of the document to be created, or null if the document doesn't belong to one.
- qualifiedNameStr - Optional DOMString containing the qualified name, that is an optional prefix and colon plus the local root element name, of the document to be created.
- documentType - Optional DocumentType of the document to be created. It defaults to null.
 
Example:
```js
var myObject = JXON.unbuild(myObject);
```

### .stringToXml(xmlString)
**Wrapper over DOMParser.parseFromString, converts string to XML document.**
- xmlString - XML string to convert to XML document

### .xmlToString(xmlObj)
**Wrapper over XMLSerializer.serializeToString, converts XML document to string.**
- xmlObj - XML document to convert to XML string

### .each(obj, callback[, thisArg])
**Helper method to iterate node(s).**  
In case that there is only one children node, JXON will return object. For multiple children it will return array. This method will always iterate nodes as array.
- obj - array or object to iterate
- callback - function to execute for each element
- thisArg - optional. Value to use as this when eecuting callback

Example:
```js
var jx = jxon.stringToJs('<val>foo</val>');
jxon.each(jx.val, function(val) {
    assert(val, 'foo');
});
```

# CHANGELOG

## 2.0.0

changes from version 1.x to 2.0 include:

* (breaking) more usefull default settings (see above)
* (breaking) stringify Dates to ISO format instead of GMT
* improved xml namespace handling on node and browsers
* renamed main source file to `jxon.js`

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