Launch Week Day 1: Socket for Jira Is Now Available.Learn More
Socket
Book a DemoSign in
Socket

xml-convert

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xml-convert

Another simple xml converter from js objects.

latest
Source
npmnpm
Version
2.0.0
Version published
Weekly downloads
55
292.86%
Maintainers
1
Weekly downloads
 
Created
Source

xml-convert

Another simple xml converter from js objects.

Usage

This package only exports one function obj2xml where you can pass a structured javascript object and it outputs some xml.

The input object must be structured as follows:

export interface XMLObject {
  tag: string
  attrs?: { [attr: string]: string | number }
  content?: string | XMLObject[]
}

Content can either be a string (for just a text inside xml tags) or multiple other XMLObjects in an array.

Example

import { obj2xml } from 'xml-convert'

obj2xml({
  tag: 'test',
  attrs: { a: 'b', b: 'c', d: 1 },
  content: [
    {
      tag: 'test2',
      attrs: { e: 'f' },
      content: [{ tag: 'test2_1', attrs: { g: 'h' } }],
    },
    { tag: 'test3', attrs: { f: 'g' }, content: 'nested' },
  ],
})

/*
<?xml version="1.0" encoding="utf-8"?>
<test a="b" b="c" d="1">
  <test2 e="f">
    <test2_1 g="h"/>
  </test2>
  <test3 f="g">nested</test3>
</test>
*/

Keywords

xml

FAQs

Package last updated on 17 Jun 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