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-to-js

v0.0.7

Published

Translates an XML string into a JavaScript object

Downloads

12

Readme

xml-to-js

New in 0.0.5 Added support for empty spaces and the following characters where they occur as content between tags: ! ? ( ) - . & % @ : $
The following value type will no longer cause an error: !i-hate [email protected]

The object of this module is to provide a direct translation of XML into a JavaScript object. The input paramaters are fairly strict which I'm hoping will result in a straight-forward and lightning fast, if somewhat specialized, module. The XML can only contain "container" tags and key value tags, for example:

var x = '<maps><china><ancient><map1>changsha</map1><map2>xian</map2></ancient><modern><map1>Shanghai</map1></modern></china></maps>';

The main function is xmlToJs which is used thusly:

var makeObject = require('xml-to-js').xmlToJs;

makeObject(x, function(object) {
  console.log(object);
})

Which renders the following object:

{ maps: { china: { ancient: { map1: 'Changsha', map2: 'xian' }, modern: { map1: 'Shanghai' } } } }

The function rawData spits out the data used to make the object and populate it's key-value fields, it returns an object with an "index" property that contains all the container data, and a "values" property that contains the key-value data:

var getData = require('xml-to-js').rawData;

getData(x, function(data) {
  console.log(data.index);
  res.render('index');
})

//=>{ index: [ [ { value: 'maps', parent: undefined } ],[ { value: 'china', parent: 'maps' } ],[ { value: 'ancient', parent: 'china' },{ value: 'modern', parent: 'china' } ] ],values: [ { level: 3, key: 'map1', parent: 'ancient', value: 'changsha' }, { level: 3, key: 'map2', parent: 'ancient', value: 'xian' },{ level: 3, key: 'map1', parent: 'modern', value: 'shanghai' } ] }

If for some reason you want to obtain an empty object, that is, a JS object without the key-value pairs contained in the XML, use the emptyObject function:

var emptyObject = require('xml-to-js').emptyObject;

emptyObject(x, function(object) {
  console.log(object);
})

//=> { maps: { china: { ancient: {}, modern: {} } } }

If your XML object has multiple root nodes you can use xmlToJsArray to return an array of objects for each root node:

var xml = '<people><john><place>2</place></john><mary><place>1</place></mary></people><things><cleanup>broom</cleanup><records>penpaper</records></things>'

var makeObjectArray = require('xml-to-js').xmlToJsArray;

makeObjectArray(xml, function (object) {
console.log(object);
})

//=> [ { people: { john: { place: '2' }, mary: { place: '1' } } }, { things: { cleanup: 'broom', records: 'penpaper' } } ]