Created
February 1, 2010 01:56
-
-
Save josher19/291391 to your computer and use it in GitHub Desktop.
xml to javascript
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
// Get leaf data | |
// or could iterate over leaf.attributes | |
function show(leaf,ind) { return [leaf.tagName, leaf.getAttribute ? leaf.getAttribute("data") : leaf.textContent]; } | |
function grab(node,indented) { | |
var ind=indented||""; | |
return Array.prototype.slice.call(node).map( function(item) { | |
var others = ""; | |
if (item.childNodes && item.childNodes.length > 0) others = grab(item.childNodes, ind + " "); | |
return ind + show(item).join(": ") +"\n"+ others | |
}).join(""); | |
} | |
// get XML | |
var x = document.childNodes; | |
// walk XML for data | |
grab(x[0].childNodes); |
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
var slice = Array.prototype.slice | |
/** parsexml: convert google weather xml to Javascript object */ | |
function parsexml(xml) { return parsed(xml[0]) } | |
/** parsed(node): turn attributes and children into Objects or Arrays (if defined more than once) */ | |
function parsed(node){ var here=getAttr(node); if (node.nodeValue) here.text = node.nodeValue; here.nodeName = node.nodeName; if (node && node.childNodes) { here.length = node.childNodes.length; slice.apply( node.childNodes ).forEach(function(subnode,ndx) { var p=parsed(subnode); if (typeof(p) != "string") here[ndx] = p; if (subnode.tagName) { setTo( here, subnode.tagName, p); } else { setTo(here, 'text', p.text); } } )}; return here; } | |
/** getAttr(node): return attributes of the node as an Object */ | |
function getAttr(node) { var obj={}; if (!node) return obj; var list=node.attributes||[]; if(list.length===1&&list[0].name==="data") return list[0].value; for(var i=0; i<list.length; ++i) { obj[list[i].name] = list[i].value } ; return obj;} | |
/** setTo(obj, key, val): obj[key]=val, but if obj[key] already defined, make into an Array */ | |
function setTo(obj, key, val,skip) { if( obj[key] !== skip) { if (!obj[key].push) obj[key] = [ obj[key] ]; obj[key].push(val); } else { obj[key]=val } } | |
/** Print a node */ | |
function printNode(node) { var res=[]; for (x in node) { res.push( x + ': "' + node[x] + '"' ) }; return res; } | |
/** Print in an Object Like syntax */ | |
function objPrint(c) { return "{\n"+printNode( c ).join(",\n")+"\n}" } | |
/* | |
// Example Usage: | |
p = parsed ( document ) | |
c = p.xml_api_reply.weather.current_conditions; | |
objPrint(c); | |
// output: | |
{ | |
nodeName: "current_conditions", | |
length: "6", | |
condition: "Clear", | |
temp_f: "41", | |
temp_c: "5", | |
humidity: "Humidity: 33%", | |
icon: "/ig/images/weather/sunny.gif", | |
wind_condition: "Wind: N at 7 mph" | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment