Tuesday, February 22, 2011

Parse XML String into DOM with Javascript

You can use following function to convert any XML string into a DOM object from a browser.
function parseXML(xml) {
    xml = xml.replace(/^[\s\n\r\t]*[<][\?][xml][^<^>]*[\?][>]/, "");
    var doc;
    if (window.ActiveXObject) {
        doc = new ActiveXObject("Microsoft.XMLDOM");
        doc.async = false;
    } else
        doc = (new DOMParser()).parseFromString(xml, "application/xml");
    return doc;

Online XML to JSON Converter using Badgerfish Notation

I have hosted an online XML to JSON converter at http://gogooog.com. It has been implemented purely with Javascript. You can read about the xml to badgerfish conversion at http://ruchirawageesha.blogspot.com/2011/01/xml-to-badgerfish-converter-in.html.