Created
April 16, 2013 05:16
-
-
Save prettycode/5393546 to your computer and use it in GitHub Desktop.
JavaScript/JSON -> XHTML serializer.
This file contains hidden or 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
<!DOCTYPE html> | |
<html xmlns:pretty="http://prettycode.org/xmlns/json2xml"> | |
<head> | |
<title>Proof of Concept</title> | |
<script> | |
var XML = { | |
defaultNamespace: undefined, | |
stringify: function(ns, value, name) { | |
if (typeof ns === 'undefined') { | |
ns = this.defaultNamespace; | |
} | |
if (Array.isArray(value)) { | |
value = value.map(function(val) { | |
return XML.stringify(ns, val, name); | |
}).join(''); | |
name += 's'; | |
} | |
else if (typeof value === 'object') { | |
value = Object.keys(value).map(function(key) { | |
return XML.stringify(ns, value[key], key); | |
}).join(''); | |
} | |
var tagName = ns + ':' + name; | |
return '<' + tagName + '>' + value + '</' + tagName + '>'; | |
} | |
}; | |
</script> | |
<script> | |
var luckyNumbers = [5, 15, 19, 83]; | |
var blogEntry = { | |
datetime: 1365760300990, | |
title: "This is a test", | |
tldr: "Nothing to see here--move along.", | |
body: "Once upon a time...", | |
fortune: luckyNumbers | |
}; | |
</script> | |
<script> | |
window.onload = function() { | |
document.body.innerHTML += XML.stringify('pretty', luckyNumbers, "fortune") | |
+ XML.stringify('pretty', blogEntry, "blogEntry"); | |
}; | |
</script> | |
<style type="text/css"> | |
/* TODO these styles aren't being applied. why not? */ | |
fortunes { | |
display: block; | |
border: 1px dotted black; | |
} | |
fortune { | |
font-size: 16px; | |
font-weight: bold; | |
} | |
</style> | |
</head> | |
<body></body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment