Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/kahwee/dom2json

Converts XML to JSON
https://github.com/kahwee/dom2json

json xml

Last synced: about 2 months ago
JSON representation

Converts XML to JSON

Awesome Lists containing this project

README

        

# dom2json

[![Greenkeeper badge](https://badges.greenkeeper.io/kahwee/dom2json.svg)](https://greenkeeper.io/)

This converts DOM documents into a special blend of JSON. It was intended to be used for XML and now made to be generic to accomodate HTML documents as well. Please take a look at usage examples to see if it suits your needs.

[![Build Status](https://travis-ci.org/kahwee/dom2json.svg?branch=master)](https://travis-ci.org/kahwee/dom2json)
[![Coverage Status](https://coveralls.io/repos/github/kahwee/dom2json/badge.svg?branch=master)](https://coveralls.io/github/kahwee/dom2json?branch=master)
[![npm version](https://badge.fury.io/js/dom2json.svg)](https://badge.fury.io/js/dom2json)
[![bitHound Overall Score](https://www.bithound.io/github/kahwee/dom2json/badges/score.svg)](https://www.bithound.io/github/kahwee/dom2json)

## Support

* Browser (tested on Firefox)
* `xmldom`

## What is sacrificed?

In order for easy access to element node children, elements are grouped together.

For example, the following XML:

```xml

Latte
Chai
Mocha
Espresso
Flat White
Mint

```

Gets converted to:

```
{
"document": {
"Drinks": {
"Coffee": [
{ $value: "Latte" },
{ $value: "Mocha" },
{ $value: "Espresso" },
{ $value: "Flat White" }
],
"Tea": [
{ $value: "Chai" },
{ $value: "Mint" }
]
}
}
}
```

So there are some information loss here. Please use with caution!

## Usage example

```js
// Optional:
// const DOMParser = require('xmldom').DOMParser

const dp = new DOMParser()
let xml = dp.parseFromString(`

Hello World

Hello again`, 'text/xml')
result = dom2json(xml)
```

Results:

```json
{
"document":{
"Hello":{
"Hi":[
{
"h1":[
{
"$attrs": {
},
"$value": "Hello World"
}
],
"$attrs":{
"class": "a"
},
"$value": "Hello World"
},
{
"$attrs":{
"class": "a"
},
"$value": "Hello again"
}
],
"$attrs":{
"one":"1",
"two":"2",
"three":"3"
}
}
}
}
```