33

I am aware of this question already existing, but it has given me no luck.

I have an application which loads a physicial XML document via the following method:

jQuery.ajax({
    type: "GET",
    url: fileName,
    dataType: "xml",
    success: function (data) {
        // etc...
    }
});

I parse the XML and convert it into a string which is saved into a variable so that it can easily be stored in a database. How can I now convert the data in this variable back into an XML object so that it can be parsed as such?

Community
  • 1
  • 1
Jack Roscoe
  • 4,293
  • 10
  • 37
  • 46

3 Answers3

74

Non-jQuery version:

var parseXml;

if (window.DOMParser) {
    parseXml = function(xmlStr) {
        return ( new window.DOMParser() ).parseFromString(xmlStr, "text/xml");
    };
} else if (typeof window.ActiveXObject != "undefined" && new window.ActiveXObject("Microsoft.XMLDOM")) {
    parseXml = function(xmlStr) {
        var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(xmlStr);
        return xmlDoc;
    };
} else {
    parseXml = function() { return null; }
}

var xmlDoc = parseXml("<foo>Stuff</foo>");
if (xmlDoc) {
    window.alert(xmlDoc.documentElement.nodeName);
}

Since jQuery 1.5, you can use jQuery.parseXML(), which works in exactly the same way as the above code:

var xmlDoc = jQuery.parseXML("<foo>Stuff</foo>");
if (xmlDoc) {
    window.alert(xmlDoc.documentElement.nodeName);
}
Tim Down
  • 318,141
  • 75
  • 454
  • 536
  • 4
    +1 It's not just non-jQuery, this actually parses XML properly, unlike the jQuery parser. – Anurag Jun 16 '10 at 14:48
  • I was actually looking for a jQuery solution, although I know I did not specify. I found your answer very useful though, had not considered this method before. – Jack Roscoe Jun 16 '10 at 14:54
  • +1 in addition to Anurags's comment: jQuery messed up my valid XML (created document was just wrong) and is not a valid solution for parsing XML! It can be used to query the document, but not to create it properly – Knickedi Aug 21 '14 at 08:20
  • isn't ActiveXObject deprecated in most modern browsers? – Don Cheadle Apr 15 '16 at 18:50
  • @mmcrae: I believe it only ever worked in IE on Windows. The ActiveX branch is there as a fallback for versions of IE (version 9 and onwards have `DOMParser`), which was more of a concern in 2010 than it is now. – Tim Down Apr 18 '16 at 09:25
7

With jquery, you can use $.parseXML(str), https://api.jquery.com/jQuery.parseXML/

artrol
  • 79
  • 1
  • 1
3

If it's still in XML format you should be able to just wrap it in the jQuery function and start using jQuery to parse through it. For example:

$(xmlStringFromDB).find('foo');
patrickmcgraw
  • 2,465
  • 14
  • 8
  • If xmlStringFromDB is still a string, then that would force a string to XML each time you use it. If you need it just once, or can link all your request, you're certainly fine, but otherwise you should save that in a (temporary) variable. – Alexis Wilke May 24 '14 at 09:46