Socket
Socket
Sign inDemoInstall

xml-decoder

Package Overview
Dependencies
2
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    xml-decoder

another xml reader with build in transformations


Version published
Weekly downloads
78
increased by25.81%
Maintainers
1
Install size
143 kB
Created
Weekly downloads
 

Readme

Source

Yet another xml parser (transformations included)

Project based on ktemelkov/node-xml2json, but is about only parsing. The main feature is ability to transform target object to remove xml structures bloating.

Parsing features:

  • every complex object in hierarhy has '@tag' field with tag name.
  • xml tag value placed in '@value' field
  • Single valued tags assigned as fileds to parent object
  • attributes axis are merged or placed to '@attrs' field
  • two or more sibling tags with same name treats as array of objects(with that tags data) (example in __tests__/fixtures/simple-list)

It does not parse the following elements:

  • CDATA sections (*)
  • Processing instructions
  • XML declarations
  • Entity declarations
  • Comments

Installation

$ npm install xml-decoder

Usage

var xmldecoder = require('xml-decoder');

var xml = "<foo attr=\"value\">bar</foo>";
console.log(xml)

var obj = xmldecoder(xml, {mergeAttrs: true});
console.log(JSON.stringify(obj));

Example xml:

<root>
  <items count="2">
    <item>1</item>
    <item>2</item>
  </items>
  <value attr="a"/>
  <value attr="b"/>
  <node_a>x</node_a>
  <node_b>y</node_b>
  <item>3</item>
</root>

Configurated options:

var options = {
	mergeAttrs: true,
	toArray: ['root/items'],
	asArray: ['root/item'],
	renameTag: {
		'root/value':'values',
		'root/node_a':'nodes',
		'root/node_b':'nodes',
	}
}

Result object:

{
  "root": {
    "@tag": "root",
    "items": [
      { "@tag": "item", "@value": 1 },
      { "@tag": "item", "@value": 2 }
    ],
    "values": [
      { "@tag": "value", "attr": "a" },
      { "@tag": "value", "attr": "b" }
    ],
    "nodes": [ "x", "y" ],
    "item": [ 3 ]
  }
}

Options

  • mergeAttrs, bool (default: felse) - flag to merge attrs with single valued child tags in common structure or not
  • asArray, array of full paths in xml - force array for tag value
  • toArray, array of full paths in xml - attributes of target tag ignored, target tag becomes array, child tags become array values
  • rename, key-value of full path to new field name - rename (former renameTag extended to attrs)
  • typecast, key-value of full path to type - override auto type casting
    typecast: false, // disable auto typecasting
    
    typecast: [
        "path/to/tag": "number",
        'path/to/@attr': "string" // attributes prefixed with @
    ]
    

Keywords

FAQs

Last updated on 02 May 2021

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc