0.1.11 • Published 6 years ago

convertjson2xml v0.1.11

Weekly downloads
15
License
ISC
Repository
github
Last release
6 years ago

convertJSON2XML Build Status

Coverage Status

A small library that converts json to xml.

Installation

npm install convertjson2xml

Usage

  const jsonToXML = require('convertjson2xml');

  let xml = jsonToXML({
    a: 1,
    b: [2,3],
    c: [],
    d: {},
    e: '',
  });

  console.log(xml);

Output should be

  <root>
    <a>1</a>
    <b>2</b>
    <b>3</b>
    <c></c>
    <d></d>
    <e/>
  </root>

To put attributes on a tag, use the '@' field:

  let xml = jsonToXML({
    a: {
      '@': {
        'attribute': 'true',
      },
      value: 1,
    },
    b: [2,3],
    c: {
      '@': {
        anotherAttribute: 'true',
      },
      '_': 'insideC',
    },
  });

  console.log(xml);

Output should be

  <root>
    <a attribute='true'>
      <value>1</value>
    </a>
    <b>2</b>
    <b>3</b>
    <c anotherAttribute='true'>insideC</c>
  </root>

You can also change the root field by inserting it as the second argument:

  let xml = jsonToXML({
    a: {
      '@': {
        'attribute': 'true',
      },
      value: 1,
    },
    b: [2,3],
    c: {
      '@': {
        anotherAttribute: 'true',
      },
      '_': 'insideC',
    },
  },'thisisthenewroot');

  console.log(xml);

Output should be

  <thisisthenewroot>
    <a attribute='true'>
      <value>1</value>
    </a>
    <b>2</b>
    <b>3</b>
    <c anotherAttribute='true'>insideC</c>
  </thisisthenewroot>

Tests

npm test

0.1.11

6 years ago

0.1.10

6 years ago

0.1.9

6 years ago

0.1.8

6 years ago

0.1.7-b

6 years ago

0.1.7-a

6 years ago

0.1.7

6 years ago

0.1.6

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago