Skip to content

Instantly share code, notes, and snippets.

@feedmypixel
Created November 27, 2012 10:41
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save feedmypixel/4153569 to your computer and use it in GitHub Desktop.
Convert XML to JSON. This is a version that provides a JSON object without the attributes and places textNodes as values rather than an object with the textNode in it.
/**
* Originally from http://davidwalsh.name/convert-xml-json
* This is a version that provides a JSON object without the attributes and places textNodes as values
* rather than an object with the textNode in it.
* 27/11/2012
* Ben Chidgey
*
* @param xml
* @return {*}
*/
function xmlToJson(xml) {
// Create the return object
var obj = {};
// text node
if (4 === xml.nodeType) {
obj = xml.nodeValue;
}
if (xml.hasChildNodes()) {
for (var i = 0; i < xml.childNodes.length; i++) {
var TEXT_NODE_TYPE_NAME = '#text',
item = xml.childNodes.item(i),
nodeName = item.nodeName,
content;
if (TEXT_NODE_TYPE_NAME === nodeName) {
//single textNode or next sibling has a different name
if ((null === xml.nextSibling) || (xml.localName !== xml.nextSibling.localName)) {
content = xml.textContent;
//we have a sibling with the same name
} else if (xml.localName === xml.nextSibling.localName) {
//if it is the first node of its parents childNodes, send it back as an array
content = (xml.parentElement.childNodes[0] === xml) ? [xml.textContent] : xml.textContent;
}
return content;
} else {
if ('undefined' === typeof(obj[nodeName])) {
obj[nodeName] = xmlToJson(item);
} else {
if ('undefined' === typeof(obj[nodeName].length)) {
var old = obj[nodeName];
obj[nodeName] = [];
obj[nodeName].push(old);
}
obj[nodeName].push(xmlToJson(item));
}
}
}
}
return obj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment