Fun with XML and AJAX

by deanpeters

HTML

<!DOCTYPE html>
<html>
    <head>
        <title>Parsing xml with jquery and ajax | faddap | if-act.net</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    </head>
    <body>
        <h1>Parsing XML/RSS with jQuery and Ajax</h1>
        <div id="processing"></div>
        <div id="recursive"></div>
        
<script type="text/javascript">
  var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-442503-9']);
    _gaq.push(['_setDomainName', 'deanpeters.com']);
    _gaq.push(['_setCampNameKey', 'jsfiddle']);    
    _gaq.push(['_setCampMediumKey', navigator.userAgent ]); 
    _gaq.push(['_setCampSourceKey', 'internet']);   
    _gaq.push(['_setCampTermKey', 'javascript']);       
    _gaq.push(['_setCampContentKey', document.title ]); 
    _gaq.push(['_trackPageview']);

     (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    })();
</script>
    </body>
</html>

JavaScript

alert("Script not ready for prime-time yet -- \n\nneed to create a 'generate' RSS\nw/jQuery (from Json) to read from same domain\n ... or figure out CORS on jsfiddle!-)");

/* 
 * http://if-act.net/demos/parsing-xml-with-jquery-and-ajax/
 */
var getXML = function(url) {
    var xmlDoc = null;
    $.ajax({
        type: "GET",
        url: url,
        dataType: "xml",
        async: false,
        success: function(xml) {
            xmlDoc = $(xml);
        },
        error: function(jqXHR, text, errorMsg) {
            console.log("The following error occured: "+errorMsg);
        }
     });
     return xmlDoc;
}

function writeXML(node, niveau) {
    // indention for better formatting
    var indent = "&nbsp;&nbsp;";
    if (niveau == null) {
        niveau = 0;
    }
    for (var i=0; i<niveau; i++) {
        indent += indent;
    }
    
    // actual implmentation
    $("#recursive").append("<br />"+indent+"["+node.get(0).nodeName+"]");
    if (node.children().length) {
        niveau++;
        node.children().each(function() {
            write($(this), niveau);
        });
    } else {
        $("#recursive").append(node.text()/*+"[\\"+node.get(0).nodeName+"]"*/);
    }
    // Optional line - closes all the tags, but fucks up the formatting. (the indent should be somehow splitted)
    //$("body").append("[\\"+node.get(0).nodeName+"]<br />");
}
   
    
    var link = "http://if-act.net/faddap/categories/javascript.xml";
    var xmlDoc = getXML(link);
    
    // Procesing by known structure
    xmlDoc.find('item').each(function(){
        $(this).children().each(function(){
            // this refers to xmlElement, while $(this) is a jQuery object
            $("#processing").append(this.nodeName+" - "+$(this).text()+"<br />");
        });
    });
    
    // Simple recursive printing of all the nodeNames and nodeValues.
    write(xmlDoc);