npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

xml-mapping

v1.7.2

Published

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

Downloads

9,846

Readme

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 XMLMapping = require('xml-mapping');

var json = XMLMapping.load('<key>value</key>');
var xml  = XMLMapping.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
  • specialChar - string - Set the first character of XML tag ($t, $text, $cd, $cdata, $e, $element, $c, $comment); default : $
  • longTag - boolean - Use long names tags($text, $element, $cdata, $comment) rather than short names ($t, $cd, $e, $c); default : false
  • comments - boolean - Flag to ignore comments, if false all the comments will be ignored : default : true
  • parserInfos - boolean - Flag to add some attributes generated by the parser (order, line, column, name), if true informations are inserted in the attributes list of all tags : default : false
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, Object options)

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

###Options

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

  • indent - boolean - Flag to throw errors of the SAX Parser ; default : false
  • header - boolean - Flag to add XML header; default : false
  • version - string - Set version attribute of XML header (see header flag); default : 1.0
  • encoding - string - Set encoding attribute of XML header (see header flag); default : UTF-8
  • specialChar - string - Set the first character of XML tag ($t, $text, $cd, $cdata, $e, $element, $c, $comment); default : $

tojson(String xml)

Alias of load.

toxml(Object json)

Alias of dump.

Also

  • https://github.com/estheban/node-json2xml
  • https://github.com/buglabs/node-xml2json

License

MIT/X11

Bitdeli Badge