+ Reply to Thread
Results 1 to 1 of 1

Thread: struggling to parse xml

  1. #1
    cGamez010's Avatar
    cGamez010 is offline x10Hosting Member cGamez010 is an unknown quantity at this point
    Join Date
    Apr 2008
    Location
    Wonderboom South, Pretoria, Gauteng, South Africa.
    Posts
    37

    Talking struggling to parse xml

    hi again, having trouble here parsing the following xml into multiple (1/2) dimensional array...
    Code:
    <?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 &gt;= &quot;2010-01-01 00:00:00&quot; AND timestamp_LeadCreated &lt;= &quot;2010-01-31 23:59:59&quot; GROUP BY text_Consumer ORDER BY bigint_LeadCount DESC LIMIT 10;</sql>
    </root>
    here is the function thats supposed to parse it, but only returns an array with empty values.
    Code:
    // xml parser, battling to parse the xml returned from the post and get requests
    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++) {
                    if (rows1[r].attributes) {
                        arrMonths[i] = rows1[r].attributes.getNamedItem("name").nodeValue;
                        i++;
                    }
                    var months = rows1[r].getElementsByTagName("month");
                    for (var c = 0; c < months.length; c++) {
                        var month = months[c];
                        if (month.attributes) {
                            arrMonths[i] = new Array();
                            for (n = 0; n > month.attributes.length; 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++;
                    }
                    var records = rows2[r].getElementsByTagName("record");
                    for (var c = 0; c < records.length; c++) {
                        var record = records[c];
                        if (record.attributes) {
                            arrStats[x] = new Array();
                            for (e = 0; e > record.attributes.length; e++) {
                                arrStats[x][e] = record.attributes[e].value;
                            }
                            x++;
                        }
                    }
                }
                alert(arrMonths.join(",",";")+"\n"+arrStats.join(",",";"));
                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;
        }
    }
    what am i not doing correctly? a few guys here told me before that i need to use multiple dimension arrays rather than a one dimentional array with strings.

    Edit:
    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:

    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 = "&nbsp;";
    		div.appendChild(sa);
    		var sd = document.createElement("div");
    		sd.setAttribute("class","statcol1");
    		sd.innerHTML = "&nbsp;";
    		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]:"&nbsp;";
    		div.appendChild(sa);
    		var sd = document.createElement("div");
    		sd.setAttribute("class","statcol1");
    		sd.innerHTML = (arrS[i])?arrS[i][1]:"&nbsp;";
    		div.appendChild(sd);
    	}
    }
    this code can be seen in action at http://www.ferrety.co.za/fab/?p=0.
    Edit:
    awesome, i finally got the ajax working by referencing the attributes directly :D
    Last edited by cGamez010; 01-14-2010 at 09:27 AM. Reason: Automerged Doublepost
    Sincerely,
    Pierre "Greywacke" du Toit.
    Please excuse the extensive posts, I prefer to split the code into includes by category/page, rather than functions.

+ Reply to Thread

Similar Threads

  1. PHP Parse Error
    By kbjradmin in forum Programming Help
    Replies: 6
    Last Post: 04-09-2009, 10:11 AM
  2. Struggling to get my site off the ground
    By IIN Operator in forum Scripts & 3rd Party Apps
    Replies: 5
    Last Post: 03-20-2009, 01:06 PM
  3. Parse error: syntax error, unexpected
    By Fr33z4r in forum Free Hosting
    Replies: 12
    Last Post: 11-04-2007, 06:12 PM
  4. Fantastico Parse error line 31?????
    By Dazz in forum Free Hosting
    Replies: 2
    Last Post: 09-03-2007, 12:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers