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

csv2xml

v0.0.4

Published

A simple JavaScript utility for converting CSVs to XML

Downloads

4

Readme

CSV2XML

A JavaScript utility for converting CSV data to arbitrarily structured XML.

browser support

How to Use

The CSV2XML module is simply a constructor which takes a configuration object and returns a transform stream. To use the returned object, pipe in CSV text, and then pipe the resulting XML to a destination of your choice.

// example uses an XML structure from the International Aid Transparency Initiative XML Schema
// see: http://iatistandard.org/

$ npm install csv2xml

var CSV2XML = require('csv2xml');
var fs = require('fs');

// create new parser by passing an configuration to the parser

var parser = new CSV2XML({
		primaryKey:'project_ID',
		sorted: true,
		mapping: {
			'project_ID': 'iati-activities/iati-activity/iati-identifier/text()',
			'transaction_values': 'iati-activities/iati-activity/transaction/value/text()',
			'transaction_type_code': 'iati-activities/iati-activity/transaction/transaction-type/@code',
			'transaction_type_name': 'iati-activities/iati-activity/transaction/transaction-type/text()',
			'transaction_date': 'iati-activities/iati-activity/transaction/transaction-date/text()',
			'project_title': 'iati-activities/iati-activity/title/text()',
			'ad_sector_name': 'iati-activities/iati-activity/sector/text()',
			'ad_sector_code': 'iati-activities/iati-activity/sector/@code',
			'precision_code': 'iati-activities/iati-activity/location/coordinates/@precision',
			'geoname_ID': 'iati-activities/iati-activity/location/gazetteer-entry/@gazetteer-ref',
			'location_type': 'iati-activities/iati-activity/location/location-type/text()',
			'location_code': 'iati-activities/iati-activity/location/location-type/@code',
			'latitude': 'iati-activities/iati-activity/location/coordinates/@latitude',
			'longitude': 'iati-activities/iati-activity/location/coordinates/@longitude',
			'placename': 'iati-activities/iati-activity/location/name/text()',
			'donors': 'iati-activities/iati-activity/participating-org/text()',
			'iati_donor_codes': 'iati-activities/iati-activity/participating-org/@ref',
			'status': 'iati-activities/iati-activity/activity-status/text()'
		}
	});

	// pipe a readable stream to the parser and then to a writable stream
	
	var inFile = fs.createReadStream('./some-file.csv');
	var outFile = fs.createWriteStream('./some-other-file.xml');

	inFile.pipe(parser).pipe(outFile);

Note: As of right now you must pass in 'sorted:true' as part of your configuration object. In the future the API will support CSVs which are sorted or unsorted by primary key (sorted CSVs allow streaming processing, which is faster and uses less memory), but this implementation only supports sorted CSVs.

Road Map

This is a very new module and the API should not be considered stable. Next steps include:

  • Supporting an optional callback API in addition to the streaming implementation
  • Supporting CSVs without explicit primary keys
  • Allowing the user to specify "constants" which are added to every XML object
  • Supporting multiple input CSVs and arbitrary joins based on user configuration