Latest: AjaMyXML.js source listing /*** ** AjaMyAjax --> XML functions library ** use with the core AJAX lib routines in ajaMyCore.js. ** ** This software provided "AS IS," but is FREE to use and share. ** For details see the as-is doc and Creative Commons license: ** http://creativecommons.org/licenses/by-sa/3.0/ ** ** See doc.txt or examples.html for syntax and more info at ** http://AjaMyAjax.com, (c) AjaMyAjax.com */ pbXMLLibReady = true; // notifies core that lib is available var pbXMLLibHeader = "[AjaMyAjax XML Lib]"; this.processXML = function() { var xmlDoc = (arguments[0]) ? arguments[0] : null; var strurl = (arguments[1]) ? arguments[1] : null; var strcontainer = (arguments[2]) ? arguments[2] : null; var stroutputFormat = (arguments[3]) ? arguments[3] : null; var strSearchElement = (arguments[4]) ? arguments[4] : null; var allnodes = null, rootname = null; if (!xmlDoc || !strcontainer) { return; } if (strSearchElement) { // all nodes by default, or set by user parameter allnodes = xmlDoc.getElementsByTagName(strSearchElement).item(0); if (allnodes) { // start with parent to list all in that node allnodes = allnodes.parentNode; } } else { allnodes = xmlDoc.getElementsByTagName('*').item(0); } if (!allnodes) { aja_empty(strcontainer); if (pbshowAlertMsgs) { aja_alertMsg(pbXMLLibHeader, "Sorry, cannot retrieve data requested for " + strurl); } return; } if (stroutputFormat === "NODETEXT") { // node text listing and return var nodetext = aja_getXmlNodeText(allnodes); // note: no \r\n between IE6 node text values, only a space. // if your xml is simple enough, this replace should work: if (!window.XMLHttpRequest) { nodetext = nodetext.replace(/\s/g, "<br\>"); } else { // Firefox, Opera support this nodetext = nodetext.replace(/\r|\n|\r\n/g, "<br\>"); } aja_fill(strcontainer, nodetext); nodetext = null; return; } if (!strSearchElement) { // find first valid root name to exclude from header rootname = xml_getRootName(allnodes.childNodes); } // node processing with private variables used to form output // note: code and functions localized to increase speed var htable = "", namestr = "", namecnt = 0, namearray = [], parentarray = []; var valuearray = [], origval = "", foundrowdata = false; var reglist = "<table id='myTableID'><tr id='myRowID'>"; // call embedded function below; recursive use processes all child nodes (if *) listNodes(allnodes); function listNodes(allnodes) { for (var i=0; i < allnodes.childNodes.length; i++) { var node = allnodes.childNodes[i]; if (node.nodeType == 1 && node.nodeName !== rootname) { // Element node if (stroutputFormat !== "CDATA") { reglist += "<th id='myHeaderID'>" + node.nodeName + "</th>"; namearray.push("<th id='myHeaderID'>" + node.nodeName + "</th>"); if (namestr.indexOf(node.nodeName) == -1) { // add only unique names to header string namestr += "<th id='myHeaderID'>" + node.nodeName + "</th>"; namecnt++; } } } // trim node for null string comparison var nodetemp = node.nodeValue; if (nodetemp) { nodetemp = nodetemp.replace(/^\s+|\s+$/g,''); } if (node.nodeType == 3 && nodetemp !== "") { // Text node if (stroutputFormat !== "CDATA") { var valuetmp = origval = node.nodeValue; valuetmp = valuetmp.toLowerCase(); var linkstart = valuetmp.indexOf("http://"); if (linkstart != -1) { // add anchor if appropriate valuetmp = xml_addAnchorTag(valuetmp, origval, linkstart); } else { // no anchor, restore case valuetmp = origval; } valuetmp = "<td>" + valuetmp + "</td>"; reglist += valuetmp; valuearray.push(valuetmp); valuetmp = null; foundrowdata = true; } } if (node.nodeType == 4) { // CDATA section // this parameter-set option excludes all other output var valuetmp = node.nodeValue; reglist += valuetmp; valuearray.push(valuetmp); valuetmp = null; foundrowdata = true; } if (node.hasChildNodes()) { listNodes(node); } else if (node.nodeType == 3 && foundrowdata) { if (stroutputFormat !== "CDATA") { reglist += "</tr><tr id='myRowID'>"; parentarray.push("<th id='myHeaderID'>" + node.parentNode.nodeName + "</th>"); foundrowdata = false; } } } } // end of listNodes reglist += "</tr></table>"; if (stroutputFormat === "HTABLE") { // horizontal table output // header cells from name string or array if needed if (namearray.length == parentarray.length || namecnt < 3) { namecnt--; // adjust for zero loop counter below htable = "<table id='myTableID'><tr id='myRowID'>" + namestr + "</tr>"; } else { // unequal header array means nested elements namecnt = parentarray.length; htable = "<table id='myTableID'>"; for (var i=0; i < namecnt; i++) { htable += parentarray[i]; } } // format output in conventional horizontal table style // add row elements up to number displayed in header var valuecnt = 0, valuelen = valuearray.length; for (var i=0; i < valuelen; i++) { if (valuecnt == 0) { htable += "<tr id='myRowID'>"; } // node value data here htable += valuearray[i]; if (valuecnt == namecnt) { htable += "</tr>"; valuecnt = 0; } else { valuecnt++; } } htable += "</table>"; aja_fill(strcontainer, htable); } else { if (stroutputFormat === "ULIST") { // transform into a basic unordered list reglist = reglist.replace(/(\<table>|\<\/table>)/gi, ""); reglist = reglist.replace(/(\<th id='myHeaderID'>)/gi, "<th>"); reglist = reglist.replace(/(\<tr id='myRowID'>)/gi, "<tr>"); reglist = reglist.replace(/(th>|tr>|td>)/gi, "li>"); reglist = reglist.replace(/\<li\>\<\li\>/gi, "<li>"); reglist = reglist.replace(/\<\/li\>\<\/li\>/gi, "</li>"); reglist = reglist.replace(/\<li\>\<\/li\>/gi, ""); } aja_fill(strcontainer, reglist); } // clean up after: try { if (strurl) strurl = null; if (strcontainer) strcontainer = null; if (stroutputFormat) stroutputFormat = null; if (strSearchElement) strSearchElement = null; listNodes = null; allnodes = null; rootname = null; htable = null; namestr = null; namecnt = null; namearray = null; parentarray = null; valuearray = null; origval = null; foundrowdata = null; reglist = null; } finally { xmlDoc = null; } } xml_addAnchorTag = function(strmod, strbase, linkstart) { if (strmod && strbase) { if (linkstart > 0) { // if any text before the hypertext, // remove link and display that text var srchtmp = strbase.substr(0, linkstart); var origsubstr = srchtmp; srchtmp = srchtmp.toLowerCase(); if (srchtmp.indexOf("href") == -1) { strbase = origsubstr; strmod = strmod.replace(srchtmp, ""); } } strmod = "<a href='" + strmod + "'>" + strbase + "</a>"; srchtmp = null; origsubstr = null; } return strmod; } xml_getRootName = function(xmlnode) { var getrootname = null; for (var i=0; i < xmlnode.length; i++) { getrootname = xmlnode[i].nodeName; if (getrootname.indexOf("#") == -1) { break; } } return getrootname; }