1

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?

Gus
  • 186
  • 1
  • 9
  • 1
    You might want to make it clear that you want to do more than simply parsing and serializing XML. – Tim Down Aug 12 '10 at 21:17

3 Answers3

3

jquery has really great xml support.

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

Update:

The above link doesn't work anymore. See this answer.

Charlie
  • 22,886
  • 11
  • 59
  • 90
hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
  • 2
    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 Aug 11 '10 at 21:42
  • @tim are you saying the "$(xml).find("Tutorial").each(function(){...})" stuff is not jquery, but rather the underlying browser? – hvgotcodes Aug 11 '10 at 21:57
  • 1
    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 Aug 12 '10 at 16:02
  • 2
    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 Aug 12 '10 at 21:30
  • because jQuery use native browser *HTML* engine, $('').html() gives you '' insted of ''. It is troublesome in some cases and should be taken into consideration if you have legacy code that treats xml as it should be - case sensitive. – Bart Jul 07 '14 at 09:44
  • This link is not accessible any more. – Charlie Jun 15 '18 at 07:01
0

http://code.google.com/p/marknote/

Please follow the above link. Its pretty good. When I tried to parse and traverse it with built-in XML parser of browser I faced problems with older versions of IE (before IE 9).

0

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
  • 318,141
  • 75
  • 454
  • 536
  • 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 Aug 12 '10 at 12:16
  • OK. But jQuery doesn't do namespaces either. – Tim Down Aug 12 '10 at 16:04
  • We currently already use the method above, I want to abstract it. – Gus Aug 13 '10 at 15:20
  • 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 Aug 13 '10 at 19:08