convertjson2xml

0.1.11 • Public • Published

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

Readme

Keywords

none

Package Sidebar

Install

npm i convertjson2xml

Weekly Downloads

2

Version

0.1.11

License

ISC

Unpacked Size

63 kB

Total Files

17

Last publish

Collaborators

  • rshyong