Greywacke Posted January 14, 2010 Share Posted January 14, 2010 hi,having a hard time parsing the following xml: <?xml version="1.0" encoding="ISO-8859-1"?><root> <statmonths name="list_consumerst10months"> <month text="January 2010" value="2010-01" selected="selected" /> <month text="December 2009" value="2009-12" /> <month text="November 2009" value="2009-11" /> </statmonths> <statistics name="top10consumers"> <record name="Koos Petoge, Test Region 7" leads="11" /> <record name="piet pompies, Test Region 7" leads="10" /> <record name="pierre du toit, Test Region 7" leads="3" /> <record name="test, Test Region 7" leads="3" /> <record name="Piet Petoge, Test Region 7" leads="2" /> <record name="piet poggenpoel, Test Region 7" leads="2" /> </statistics> <sql>SELECT text_Consumer, text_RegionDescription, COUNT(1) AS bigint_LeadCount FROM 6_serviceleads JOIN (1_regions) ON (6_serviceleads.bigint_RegionID = 1_regions.bigint_RegionID) WHERE timestamp_LeadCreated >= "2010-01-01 00:00:00" AND timestamp_LeadCreated <= "2010-01-31 23:59:59" GROUP BY text_Consumer ORDER BY bigint_LeadCount DESC LIMIT 10;</sql></root> here is the alertContents function that is supposed to get the attributes of the month and record tags // xml parser, battling to parse the xml returned from the post and get requestsfunction alertContents(http_request) { if (http_request.readyState == 4) { if (http_request.status == 200) { var xmldoc = http_request.responseXML; //alert(this.responseText); // parse months if available var i = 0; var arrMonths = new Array(); var rows1 = xmldoc.getElementsByTagName("statmonths"); for (var r = 0; r < rows1.length; r++) { if (rows1[r].attributes) { arrMonths[i] = rows1[r].attributes.getNamedItem("name").nodeValue; i++; } for (var c = 0; c < rows1[r].childNodes.length; c++) { var month = rows1[r].childNodes[c]; if (month.nodeName == "month") { arrMonths[i] = new Array(); for (n = 0; n > month.attributes.length; n++) { if (month.attributes[n]) arrMonths[i][n] = month.attributes[n].value; } i++; } } } // parse records var x = 0; var arrStats = new Array(); var rows2 = xmldoc.getElementsByTagName("statistics"); for (var r = 0; r < rows2.length; r++) { if (rows2[r].attributes) { arrStats[x] = rows2[r].attributes.getNamedItem("name").nodeValue; x++; } for (var c = 0; c < rows2[r].childNodes.length; c++) { var record = rows2[r].childNodes[c]; if (record.nodeName == "record") { arrStats[x] = new Array(); for (e = 0; e > record.attributes.length; e++) { if (record.attributes[e]) arrStats[x][e] = record.attributes[e].value; } x++; } } } alert(arrMonths.join(",",";")+"\n"+arrStats.join(",",";")); //if (xmldoc.getElementsByTagName("sql")[0]) if (xmldoc.getElementsByTagName("sql")[0].firstChild) //var sql = xmldoc.getElementsByTagName("sql")[0].firstChild.data; if (sql != "") alert(sql); } else { alert('There was a problem with the request.'); } document.getElementById("ajaxbg").style.visibility = "hidden"; sessrem = sesstot; }} Link to comment Share on other sites More sharing options...
Greywacke Posted January 14, 2010 Author Share Posted January 14, 2010 this code has been edited, both the alertContents and the insertstats functions... i can now retrieve numeric attributes, but not string attributes apart from the section names... wth? even tried renaming the xml attributes to statnames in case they were clashing somehow, but to no avail.i keep getting undefined as the those values...Code: function alertContents(http_request) { if (http_request.readyState == 4) { if (http_request.status == 200) { var xmldoc = http_request.responseXML; //alert(http_request.responseText); // parse months if available var i = 0; var arrMonths = new Array(); var rows1 = xmldoc.getElementsByTagName("statmonths"); for (var r = 0; r < rows1.length; r++) { var months = rows1[r].getElementsByTagName("month"); if (rows1[r].attributes) { arrMonths[i] = rows1[r].attributes.getNamedItem("name").nodeValue; i++; } for (var c = 0; c < months.length; c++) { var month = months[c]; if (month.attributes) { arrMonths[i] = new Array(); arrMonths[i][0] = month.attributes.getNamedItem("text").nodeValue; arrMonths[i][1] = month.attributes.getNamedItem("value").nodeValue; if (month.attributes.getNamedItem("selected")) arrMonths[i][2] = month.attributes.getNamedItem("selected").nodeValue; i++; } } } // parse records var x = 0; var arrStats = new Array(); var rows2 = xmldoc.getElementsByTagName("statistics"); for (var r = 0; r < rows2.length; r++) { var records = rows2[r].getElementsByTagName("record"); if (rows2[r].attributes) { arrStats[x] = rows2[r].attributes.getNamedItem("name").nodeValue; x++; } for (var c = 0; c < records.length; c++) { var record = records[c]; if (record.attributes) { arrStats[x] = new Array(); arrStats[x][0] = record.attributes.getNamedItem("statname").nodeValue; if (arrMonths[0]) { arrStats[x][1] = record.attributes.getNamedItem("leads").nodeValue; } else { arrStats[x][1] = record.attributes.getNamedItem("amount").nodeValue; } x++; } } } //alert(arrMonths+"\n"+arrStats); insertstats(arrMonths,arrStats); //if (xmldoc.getElementsByTagName("sql")[0]) if (xmldoc.getElementsByTagName("sql")[0].firstChild) //var sql = xmldoc.getElementsByTagName("sql")[0].firstChild.data; if (sql != "") alert(sql); } else { alert('There was a problem with the request.'); } document.getElementById("ajaxbg").style.visibility = "hidden"; sessrem = sesstot; }}function insertstats(arrM,arrS) { // arrM = selectid, {mName, mValue, mSelected}, etc. if (arrM != undefined) { sel = document.createElement("select"); sel.id = arrM[0]; sel.setAttribute("name",arrM[0]); sel.setAttribute("accesskey","h"); switch (sel.id) { case "list_consumerst10months": sel.setAttribute("onchange","return ajaxRequest(0);"); break; case "list_regionst10months": sel.setAttribute("onchange","return ajaxRequest(1);"); break; case "list_servicest10months": sel.setAttribute("onchange","return ajaxRequest(2);"); break; case "list_supplierst10months": sel.setAttribute("onchange","return ajaxRequest(3);"); break; } for (var i = 1; i < arrM.length; i++) { //alert(arrM[i]); var opt = document.createElement("option"); opt.text = arrM[i][0]; opt.value = arrM[i][1]; if (arrM[i][2]) opt.selected = true; try { sel.add(opt, null); // standards compliant; doesn't work in IE } catch(ex) { sel.add(opt); // IE only } } label = document.createElement("label"); label.setAttribute("for", arrM[0]); label.innerHTML = "View Mont<u>h</u>"; p = document.createElement("p"); p.appendChild(label); p.appendChild(sel); } // arrS = divid, {sName, sValue), etc. var div = document.getElementById(arrS[0]); div.innerHTML = ""; if (arrM[0] != undefined) div.appendChild(p); else { var sa = document.createElement("div"); sa.setAttribute("class","statcol2"); sa.innerHTML = " "; div.appendChild(sa); var sd = document.createElement("div"); sd.setAttribute("class","statcol1"); sd.innerHTML = " "; div.appendChild(sd); } var ta = document.createElement("div"); ta.setAttribute("class","statcol2"); ta.innerHTML = "<strong>Name</strong>"; div.appendChild(ta); var td = document.createElement("div"); td.setAttribute("class","statcol1"); td.innerHTML = "<strong>"+((arrM[0])?"Leads":"Amount")+"</strong>"; div.appendChild(td); for (i = 1; i < arrS.length+1; i++) { //alert(arrS[i]); var sa = document.createElement("div"); sa.setAttribute("class","statcol2"); sa.innerHTML = (arrS[i])?arrS[i][2]:" "; div.appendChild(sa); var sd = document.createElement("div"); sd.setAttribute("class","statcol1"); sd.innerHTML = (arrS[i])?arrS[i][1]:" "; div.appendChild(sd); }} this code can be seen in action at http://www.ferrety.co.za/fab/?p=0. Link to comment Share on other sites More sharing options...
Greywacke Posted January 14, 2010 Author Share Posted January 14, 2010 awesome, i finally got the ajax working by referencing the attributes directly Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.