Friday, January 28, 2011

XML to BadgerFish Conversion in Javascript

Recently I came across the need of converting an xml into a JSON format. There I used BadgerFish notation to transform the xml into JSON.

You can use following Javascript function to convert any DOM node into the relevant BadgerFish notation. You can convert any XML string into a DOM object using method described at Parse XML String into DOM with Javascript

Please find the hosted version of XML to Badgerfish converter at gogooog.com
function xml2bf(node) {
    var json = {};
    var cloneNS = function(ns) {
        var nns = {};
        for (var n in ns) {
            if (ns.hasOwnProperty(n)) {
                nns[n] = ns[n];
            }
        }
        return nns;
    };
    var process = function (node, obj, ns) {
        if (node.nodeType === 3) {
            if (!node.nodeValue.match(/[\S]+/)) return;
            if (obj["$"] instanceof Array) {
                obj["$"].push(node.nodeValue);
            } else if (obj["$"] instanceof Object) {
                obj["$"] = [obj["$"], node.nodeValue];
            } else {
                obj["$"] = node.nodeValue;
            }
        } else if (node.nodeType === 1) {
            var p = {};
            var nodeName = node.nodeName;
            for (var i = 0; node.attributes && i < node.attributes.length; i++) {
                var attr = node.attributes[i];
                var name = attr.nodeName;
                var value = attr.nodeValue;
                if (name === "xmlns") {
                    ns["$"] = value;
                } else if (name.indexOf("xmlns:") === 0) {
                    ns[name.substr(name.indexOf(":") + 1)] = value;
                } else {
                    p["@" + name] = value;
                }
            }
            for (var prefix in ns) {
                if (ns.hasOwnProperty(prefix)) {
                    p["@xmlns"] = p["@xmlns"] || {};
                    p["@xmlns"][prefix] = ns[prefix];
                }
            }
            if (obj[nodeName] instanceof Array) {
                obj[nodeName].push(p);
            } else if (obj[nodeName] instanceof Object) {
                obj[nodeName] = [obj[nodeName], p];
            } else {
                obj[nodeName] = p;
            }
            for (var j = 0; j < node.childNodes.length; j++) {
                process(node.childNodes[j], p, cloneNS(ns));
            }
        } else if (node.nodeType === 9) {
            for (var k = 0; k < node.childNodes.length; k++) {
                process(node.childNodes[k], obj, cloneNS(ns));
            }
        }
    };
    process(node, json, {});
    return json;
}

4 comments:

  1. Thank God. Finally I found it.

    ReplyDelete
  2. Nicely done, thanks! I will agree with the other poster "Finally I found it (a nice implementation)". Perhaps you could post this on github or a place like it to make it easier to find?

    ReplyDelete
  3. Nice post this helped me to convert xml string to JSON badgerfish, Thanks indeed :)

    ReplyDelete
  4. Is there a chance to get CDATA-tags also?

    ReplyDelete