🚀 Socket Launch Week 🚀 Day 3: Socket Acquires Coana.Learn More
Socket
Sign inDemoInstall
Socket

xml-decoder

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xml-decoder

another xml reader with build in transformations

0.1.4
latest
npm
Version published
Weekly downloads
105
-38.6%
Maintainers
1
Weekly downloads
 
Created
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

xml

FAQs

Package last updated on 02 May 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