Skip to content

Instantly share code, notes, and snippets.

@karthik20522
Created August 17, 2019 19:40
Show Gist options
  • Save karthik20522/4e113919793a644b2a0739b098d166e1 to your computer and use it in GitHub Desktop.
Save karthik20522/4e113919793a644b2a0739b098d166e1 to your computer and use it in GitHub Desktop.
xmp_exif_2.js
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