Skip to content

Commit

Permalink
Initial Import, version 0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Etienne Lachance committed Jun 9, 2011
1 parent 9dc281c commit 9f6beeb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./lib/json2xml');
39 changes: 39 additions & 0 deletions lib/json2xml.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Converts JSON object to XML string.
*
* @param name of the first tag
* @param json object to convert
* @return XML string
*
* Copyright(c) 2011 Etienne Lachance <[email protected]>
* MIT Licensed
*/

var json2xml = Object();

exports.toXml = function(name, json) {
var header = "<"+name+">";
var footer = "</"+name+">";
var result = "";

for(var key in json) {
switch(Object.prototype.toString.call(json[key])) {
case "[object Array]":
for(var elem in json[key]) {
result += this.toXml(key, json[key][elem], key);
}
break;
case "[object Object]":
result += this.toXml(key, json[key], key);
break;
default:
result += "<"+key+">"+json[key]+"</"+key+">";
break;
}
}
if(name != "") {
result = header+result+footer;
}

return result;
}
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "json2xml",
"description": "JSON 2 XML Parser",
"version": "0.0.1",
"author": "Etienne Lachance <[email protected]>",
"main": "./index.js",
"engines": { "node": ">= 0.1.98" },
"repository" : {
"type" : "git",
"url" : "https://[email protected]/estheban/node-json2xml.git"
},
}

0 comments on commit 9f6beeb

Please sign in to comment.