tags:

views:

48

answers:

4

The codebase keeps files in XML format and javascript receives them via ajax and currently manually parses them into element nodes by doing the "div.innerHTML" trick, but this fails in fringe cases.

Is there a javascript library with a lot of XML capabilities already built in?

+4  A: 

jquery has really great xml support.

http://www.switchonthecode.com/tutorials/xml-parsing-with-jquery

hvgotcodes
No, it hasn't. That example uses an XML document created by the browser as part of an Ajax request and then applies jQuery to the XML document. jQuery does no XML parsing in that example and has no facility for parsing XML.
Tim Down
@tim are you saying the "$(xml).find("Tutorial").each(function(){...})" stuff is not jquery, but rather the underlying browser?
hvgotcodes
No. I'm saying that the step of converting a string into an XML document ("parsing") is performed by the browser and not jQuery. jQuery provides its usual tools on top of the existing XML document to make finding and acting on particular portions of the XML document easier. The page you linked to is misusing the word "parse".
Tim Down
Splitting hairs here, but Tim is correct that jQuery doesn't "parse" XML. It delegates that to the underlying browser. What jQuery provides is a convenient abstraction for manipulating XML, which I believe is what the OP wants. So "parsing" or not, this solution is correct.
Vivin Paliath
+1  A: 

You should really use jQuery.

Daniel Moura
No, you really shouldn't for parsing XML.
Tim Down
A: 

There's no need for a library. All modern browsers have a built-in XML parser, and all you need is a function to detect which flavour of parser the user's browser has, such as the following:

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 xml = parseXml("<foo>Stuff</foo>");
if (xml) {
    window.alert(xml.documentElement.nodeName);
}
Tim Down
I'm aware that you can do it using straight up XML, but theres slight differences between IE 6-8, FF 2-3 between how it reads the XML (some put namespaces in the nodeName some don't for example). I'm not going to rewrite a good library.hvgotcodes is something like what I'm looking for. Big + if it could have XSLT or XPath support.
Gus
OK. But jQuery doesn't do namespaces either.
Tim Down
We currently already use the method above, I want to abstract it.
Gus
Abstract it how?
Tim Down
Also, your question suggests otherwise: it states that you're parsing XML by assigning it to the `innerHTML` property of an HTML element, which is not what my code is doing.
Tim Down
A: 

There's the ancient Google AJAXSLT, apparently not touched since 2008 but unlikely to have gone seriously out of date.

Tim Down