forked from geddy/geddy
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:mde/geddy
* 'master' of github.com:mde/geddy: Updated doc-generation tasks. Add geddy.utils.XML.stringify for XML output Version 0.3.4
- Loading branch information
Showing
4 changed files
with
62 additions
and
25 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
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
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,51 @@ | ||
/* This work is licensed under Creative Commons GNU LGPL License. | ||
License: http://creativecommons.org/licenses/LGPL/2.1/ | ||
Version: 0.9 | ||
Author: Stefan Goessner/2006 | ||
Web: http://goessner.net/ | ||
*/ | ||
function json2xml(o, tab) { | ||
var toXml = function(v, name, ind) { | ||
var xml = ""; | ||
if (v instanceof Array) { | ||
for (var i=0, n=v.length; i<n; i++) | ||
xml += ind + toXml(v[i], name, ind+"\t") + "\n"; | ||
} | ||
else if (typeof(v) == "object") { | ||
var hasChild = false; | ||
xml += ind + "<" + name; | ||
for (var m in v) { | ||
if (m.charAt(0) == "@") | ||
xml += " " + m.substr(1) + "=\"" + v[m].toString() + "\""; | ||
else | ||
hasChild = true; | ||
} | ||
xml += hasChild ? ">" : "/>"; | ||
if (hasChild) { | ||
for (var m in v) { | ||
if (m == "#text") | ||
xml += v[m]; | ||
else if (m == "#cdata") | ||
xml += "<![CDATA[" + v[m] + "]]>"; | ||
else if (m.charAt(0) != "@") | ||
xml += toXml(v[m], m, ind+"\t"); | ||
} | ||
xml += (xml.charAt(xml.length-1)=="\n"?ind:"") + "</" + name + ">"; | ||
} | ||
} | ||
else { | ||
xml += ind + "<" + name + ">" + v.toString() + "</" + name + ">"; | ||
} | ||
return xml; | ||
}, xml=""; | ||
for (var m in o) | ||
xml += toXml(o[m], m, ""); | ||
return tab ? xml.replace(/\t/g, tab) : xml.replace(/\t|\n/g, ""); | ||
} | ||
|
||
exports.XML = new function XML() { | ||
this.stringify = function(o) { | ||
return json2xml(o); | ||
}; | ||
}; |
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