Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

xml-converter

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xml-converter

A module for converting between XML format and JavaScript objects.

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

xml-converter

A module for converting between XML format and JavaScript objects.

Documentation in Russian

Requirements

  • Node.js version >=16.

Installation

npm i xml-converter

Examples of using

Parsing XML to JavaScript object.

const { parseXML } = require( 'xml-converter' );

const xml = `<root>
<child>
    <tag>string value</tag>
    <tag>78</tag>
</child>
</root>`;

const object = parseXML( xml );
Result
//object
{
  root: [
    {
      value: '',
      attrs: {},
      self: false,
      child: [
        {
          value: '',
          attrs: {},
          self: false,
          tag: [
            {
              value: 'string value',
              attrs: {},
              self: false,
            },
            {
              value: '78',
              attrs: {},
              self: false,
            }
          ]
        }
      ]
    }
  ],
}

Parsing JavaScript object to XML.

const { parseObject } = require( 'xml-converter' );

const object = {
    root: [
      {
        value: '',
        attrs: {},
        self: false,
        child: [
          {
            value: '',
            attrs: {},
            self: false,
            tag: [
              {
                value: 'string value',
                attrs: {},
                self: false,
              },
              {
                value: '78',
                attrs: {},
                self: false,
              }
            ]
          }
        ]
      }
    ],
  }

const xml = parseObject( object, 2 );
Result
//xml
`<root>
<child>
  <tag>string value</tag>
  <tag>78</tag>
</child>
</root>`

Arguments

parseXML( xml )

ArgumentTypeDescription
xmlstringValid xml string.

parseObject( object, indentSize )

ArgumentTypeDescription
objectobjectValid object described in the next section.
indentSizenumberIndent size for xml string.

Fields and type of the object returned from the parseXML().

FieldTypeDescription
valuestringValue of the XML tag.
attrsobjectObject with tag's attributes { attributeName: 'attributeValue' }.
selfbooleanFlag indicating whether the tag is self-closing.
[tag]arrayArray with child-tags with same tag name.
Example
//object
{
  root: [ // root tag name
    { // object, root tag
      value: '', // root tag value
      attrs: {}, // root tag attributes
      self: false,
      child: [ // child tag name
        { // object, child tag
          value: '', // child tag value...
          attrs: {},
          self: false,
          tag: [
            {
              value: 'string value',
              attrs: {},
              self: false,
            },
            {
              value: '78',
              attrs: {},
              self: false,
            }
          ]
        }
      ]
    }
  ],
}

Keywords

FAQs

Package last updated on 20 Nov 2021

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc