Created
August 16, 2013 08:58
-
-
Save shivergard/6248358 to your computer and use it in GitHub Desktop.
YQL style dom mapping
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
console.clear(); | |
function mapDOM(element, json) { | |
var treeObject = {}; | |
// If string convert to document Node | |
if (typeof element === "string") { | |
if (window.DOMParser) | |
{ | |
parser = new DOMParser(); | |
docNode = parser.parseFromString(element,"text/xml"); | |
} | |
else // Microsoft strikes again | |
{ | |
docNode = new ActiveXObject("Microsoft.XMLDOM"); | |
docNode.async = false; | |
docNode.loadXML(element); | |
} | |
element = docNode.firstChild; | |
} | |
//Recursively loop through DOM elements and assign properties to object | |
function treeHTML(element, object) { | |
nodname = element.nodeName.toLowerCase(); | |
object["_snd"] = nodname; | |
var nodeList = element.childNodes; | |
if (nodeList != null) { | |
if (nodeList.length) { | |
object[nodname] = []; | |
for (var i = 0; i < nodeList.length; i++) { | |
if (nodeList[i].nodeType == 3) { | |
if (typeof object[nodname] == 'undefined'){ | |
console.log(object); | |
console.log(nodname); | |
}else{ | |
if (typeof object[nodname].push == 'undefined'){ | |
console.log('undefined push'); | |
console.log(nodeList[i].nodeValue); | |
}else{ | |
object[nodname].push(nodeList[i].nodeValue); | |
} | |
} | |
} else { | |
if (typeof object[nodname] == 'undefined'){ | |
object[nodname] = {}; | |
console.log(object); | |
} | |
if (typeof object[nodname].push != 'undefined'){ | |
object[nodname].push({}); | |
if (typeof object[nodname] == 'undefined' || typeof object[nodname]['_snd'] == 'undefined'){ | |
console.log(nodeList[i].nodeName.toLowerCase()); | |
console.log(nodeList[i].nodeName.toLowerCase()); | |
object[nodeList[i].nodeName.toLowerCase()] = {}; | |
object[nodname][object[nodeList[i].nodeName.toLowerCase()].length -1] = {}; | |
treeHTML(nodeList[i], object[nodname][object[nodeList[i].nodeName.toLowerCase()].length -1]); | |
}else{ | |
treeHTML(nodeList[i], object[nodname][object[object[nodname]['_snd']].length -1]); | |
} | |
}else{ | |
console.log('hasUndefined'); | |
console.log(object[nodname]); | |
console.log(nodname); | |
} | |
} | |
} | |
} | |
} | |
if (element.attributes != null) { | |
if (element.attributes.length) { | |
object["attributes"] = {}; | |
for (var i = 0; i < element.attributes.length; i++) { | |
object["attributes"][element.attributes[i].nodeName] = element.attributes[i].nodeValue; | |
} | |
} | |
} | |
} | |
treeHTML(element, treeObject); | |
return (json) ? JSON.stringify(treeObject) : treeObject; | |
} | |
var initElement = document.getElementsByTagName("body")[0]; | |
var json = mapDOM(initElement, true); | |
console.log(json); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment