forked from estheban/node-json2xml
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Etienne Lachance
committed
Jun 9, 2011
1 parent
9dc281c
commit 9f6beeb
Showing
3 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./lib/json2xml'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
}, | ||
} |