@fedyou/xml-parser
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

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: &

Readme

Keywords

Package Sidebar

Install

npm i @fedyou/xml-parser

Weekly Downloads

2

Version

1.1.0

License

MIT

Unpacked Size

6.39 kB

Total Files

5

Last publish

Collaborators

  • fedyou