1.1.0 • Published 10 months ago

@fedyou/xml-parser v1.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
10 months ago

Xml Parser

It converts JSON objects to xml, is lightweight, fast and simple, and does not use any dependencies.

Works for both commonjs and esmodules.

Use

const xmlParser = require('@fedyou/xml-parser')

const json = {
  '@elem': 'elem',
  '@atts': { hidden: false, position: 20, content: 'Some text' },
  '@childs': [
    {
      '@elem': 'child',
      '#': 'text',
      // "#": 20,
      // "#": true,
      '@childs': [
        {
          '@elem': 'sub-child'
        }
      ]
    }
  ]
}

// By default the formatting is set to false
const xml = xmlParser('root', json, { format: true })

console.log(xml)

Result:

<?xml version='1.0' encoding='utf-8'?>
<root>
  <elem hidden='false' position='20' content='Some text'>
    <child>text
      <sub-child>
    </child>
  </elem>
</root>

Another way to use JSON as an array:

const json = [
  {
    '@elem': 'child'
  },
  {
    '@elem': 'child',
    '@atts': { boolean: true }
  }
]

Abbreviations

@elem: @

#: #

@atts: $

@childs: &

1.1.0

10 months ago

1.0.0

12 months ago