Created
August 17, 2019 19:40
-
-
Save karthik20522/4e113919793a644b2a0739b098d166e1 to your computer and use it in GitHub Desktop.
xmp_exif_2.js
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
function xml2Object(xml) { | |
try { | |
var obj = {}; | |
if (xml.children.length > 0) { | |
for (var i = 0; i < xml.children.length; i++) { | |
var item = xml.children.item(i); | |
var attributes = item.attributes; | |
for(var idx in attributes) { | |
var itemAtt = attributes[idx]; | |
var dataKey = itemAtt.nodeName; | |
var dataValue = itemAtt.nodeValue; | |
if(dataKey !== undefined) { | |
obj[dataKey] = dataValue; | |
} | |
} | |
var nodeName = item.nodeName; | |
if (typeof (obj[nodeName]) == "undefined") { | |
obj[nodeName] = xml2json(item); | |
} else { | |
if (typeof (obj[nodeName].push) == "undefined") { | |
var old = obj[nodeName]; | |
obj[nodeName] = []; | |
obj[nodeName].push(old); | |
} | |
obj[nodeName].push(xml2json(item)); | |
} | |
} | |
} else { | |
obj = xml.textContent; | |
} | |
return obj; | |
} catch (e) { | |
console.log(e.message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment