🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

json-xml-parse

Package Overview
Dependencies
Maintainers
0
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-xml-parse

A library for fast JSON to XML transformations.

1.3.0
latest
Source
npm
Version published
Maintainers
0
Created
Source

Transform JSON to XML

A library for fast JSON to XML transformations.

Main Features

  • Fast Conversion
  • Lightweight
  • Dependency-Free
  • CLI and Browser Support

Options

NameTypeDefault
attrKeystr"@"
contentKeystr"#"
beautifybooltrue
selfClosingbooltrue
typeHandlerfunc(v) => [true, v]
declarationobj{ "version": "1.0" }
entityMapobj{ "<": "&lt;", ">": "&gt;", "&": "&amp;", "'": "&apos;", "\"": "&quot;"}

Usage

const parser = require('json-xml-parse');

const data = {
  SolarSystem: {
    Galaxy: "\"MilkyWay\"",
    Star: () => "Sun",
    Planet: [
      {
        "position": "1",
        "name": "<Mercury>",
        "distance": "58",
      },
      {
        "position": "2",
        "name": "Venus",
        "distance": "108"
      }
    ],
    Planet1: {
      "@": {
        position: "3",
        distance: "149"
      },
      name: "Earth"
    },
    Planet2: {
    "@": {
      position: "4",
      distance: 227,
    },
    "#": {
        name: {
          "#" : 'mars',
          "@": {
            "has-water": 'true'
          },
        },
        moon: [
          undefined,
          'Europa',
          'Deimos'
        ]
      }
    }
  }
}

const xml = parser.jsXml.toXmlString(data);
<?xml version="1.0"?>
<SolarSystem>
 <Galaxy>&quot;MilkyWay&quot;</Galaxy>
 <Star>Sun</Star>
 <Planet>
  <position>1</position>
  <name>&lt;Mercury&gt;</name>
  <distance>58</distance>
 </Planet>
 <Planet>
  <position>2</position>
  <name>Venus</name>
  <distance>108</distance>
 </Planet>
 <Planet1 position="3" distance="149">
  <name>Earth</name>
 </Planet1>
 <Planet2 position="4" distance="227">
  <name has-water="true">mars</name>
  <moon/>
  <moon>Europa</moon>
  <moon>Deimos</moon>
 </Planet2>
</SolarSystem>

Type Handling

const options = {
  typeHandler: (v) => [true, v]; 
  // return [<bool for creating tag>, <value>]
  // runs for every nested property
}

Using Options

const Options = {
  beautify: true,
  selfClosing: true,
  typeHandler: (v) => {
    if([undefined, null, ''].includes(v)) {
      return [false, v]
    }
    return [true, v]
  }
}

const data = {
  SolarSystem: {
    Galaxy: undefined,
    Star: () => null,
    Planet: 'Earth'
  }
}
const xml = parser.jsXml.toXmlString(data, Options);
<?xml version="1.0"?>
<SolarSystem>
 <Planet>Earth</Planet>
</SolarSystem>

Keywords

jsonToXml

FAQs

Package last updated on 22 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts