Socket
Book a DemoInstallSign in
Socket

xml-mapping

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xml-mapping

provide a bidirectionnal mapping between XML and JS data Structure (aka JSON)

Source
npmnpm
Version
1.5.1
Version published
Weekly downloads
1.8K
-48.77%
Maintainers
1
Weekly downloads
 
Created
Source

xml2json and json2xml for NodeJS

Build Status

It's native javascript implementation of a bidirectional converter between XML and JS data structure (aka JSON). You can convert any type of XML documents in an Javascript data structure. You can also do the reverse, converting a Javascript data structure in XML String. XML is still valid.

Contributors

Installation

With npm do:

$ npm install xml-mapping

Usage

var xm = require('xml-mapping');

var json = xm.load('<key>value</key>');
var xml  = xm.dump(json);

console.log(xml,json);
console.log(json);

Output:

<key>value</key> { key: { '$t': 'value' } }

Convention

The rules for converting XML to JSON are those used by Google in its GData protocol. More information here : http://code.google.com/apis/gdata/docs/json.html

Tests

Use nodeunit to run the tests.

$ npm install nodeunit
$ nodeunit test

API Documentation

load(String xml, Object options)

Transform a string with XML in Javascript data structure (JSON). Return Object.

###Options

Warning : options break the symmetry. This means that the JSON generated may not reproduce the same XML

  • throwErrors - boolean - Flag to throw errors of the SAX Parser ; default : false
  • nested - boolean - Flag to ignore nested tags inside text : default : false
  • comments - boolean - Flag to ignore comments, if false all the comments will be ignored : default : true
var xml = '<title>Title is <strong>important</strong></title>';
var json = XMLMapping.load(xml, { nested: true });
console.log(json);

// Should output:
// { title : { $t : 'Title is<strong>important</strong>', strong: { '$t': 'important' }} }
  • arrays - array - an array of basic XPath strings that specify XML nodes that should be array, even when there is only one such node.
var xml = '<key1>value1</key1><key2><key3>value3</key3></key2>';
var json = XMLMapping.load(xml, {
 arrays: [
  '/key1',
  '/key2/key3'
 ]
});
console.log(json);

// Should output:
// (Note that value of key1 and key3 are arrays, as specified in options)
// {"key1":[{"$t":"value1"}],"key2":{"key3":[{"$t":"value3"}]}}

dump(Object json)

Transform a Javascript data structure (JSON) in XML string. Return String.

tojson(String xml)

Alias of load.

toxml(Object json)

Alias of dump.

Also

License

MIT/X11

Bitdeli Badge

Keywords

xml

FAQs

Package last updated on 12 Nov 2013

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