Friday, 27 January 2012

JavaScript function to get XML object from string

Here's a function that you can use to load an xml object from an xml-formatted string in JavaScript:

function loadXMLFromString(text)
    {
        try //Internet Explorer
        {
            xmlDoc=new ActiveXObject('Microsoft.XMLDOM');
            xmlDoc.async='false';
            xmlDoc.loadXML(text);
        }
        catch(e)
        {
            try //Firefox, Mozilla, Opera, etc.
            {
                parser=new DOMParser();
                xmlDoc=parser.parseFromString(text,'text/xml');
            }
            catch(e) {alert(e.message);}
        }
       
        return xmlDoc;
    }


No comments: