+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: AJAX: battling to parse the xml data. Please Help!

  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 AJAX: unrelated syntax error. Please Help!

    hi, i still struggle with xml parsing, it's kinda like quantum physics in my opinion. :P

    here is the xml that i need to parse, there is always an order with leads as children. these leads have various properties, and the last 3 properties have attributes. The php can generate a different order, by service, by supplier and by region by supplying an o querystring variable to the xml generator, with values 0 through 2...

    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <root>
        <order name="Test Region 7">
            <lead>
                <id>95</id>
                <consumer>Pierre du Toit;pierre@greywacke.co.za</consumer>
                <subject>test subject</subject>
                <attribs>Test Attribute 1 = Test Value 1&lt;br /&gt;Test Attribute 2 = Test Value 3</attribs>
                <message>test message</message>
                <sent>1</sent>
                <created>2009-12-31 17:43:57</created>
                <service id="1" name="Test Service 1" cost="20" />
                <supplier id="6" name="Test Supplier 1" w3a="http://www.greywacke.co.za/" contact="Pierre du Toit" position="Graphic Design &amp; AJAX" mail="pierre@greywacke.co.za" tel="27729154799" bal="800" />
                <region id="8" name="Test Region 7" />
            </lead>
        </order>
    </root>
    below is the javascript xml parser function.

    Previous approach removed.

    i am attempting to pass the oarr array to the addorder() function looking as follows:

    Code:
    Test Region 7,95|Pierre du Toit;pierre@greywacke.co.za|test subject|Test Attribute 1 = Test Value 1<br />Test Attribute 2 = Test Value 3|test message|1|2009-12-31 17:43:57|1;Test Service 1|20|6;Test Supplier 1;|http://www.greywacke.co.za/;Pierre du Toit;Graphic Design & AJAX;pierre@greywacke.co.za;27729154799;800|8;Test Region 7
    i do the "multidimentional" arrays ready to be split by delimiter in the dhtml code. the xml parsing code needs to be ready for multiple order and lead tags however. but sofar i haven't even gotten to the addorder() function yet!
    Edit:
    okay i've edited the function a bit, but now i get of all things a syntax error pointing to the else after checking the return state 0o

    here is the updated function

    Code:
    // xml parser
    function alertContents() {
    // === BEGIN IF 0 ===
        if (http_request.readyState == 4) {
    // === BEGIN IF 1 ===
            if (http_request.status == 200) {
                var xmldoc = http_request.responseXML;
                //alert(this.responseText);
                var order = xmldoc.getElementsByTagName("order");
    // === BEGIN FOR 0 ===
                for (var o = 0; o < order.length; o++) {
                    var r = 0;
                    var olen = parseInt(order[o].childNodes.length / 2);
                    var oarr = new Array(olen);
                    oarr[r] = order[o].attributes[0].value;
                    var leads = order[o].getElementsByTagName("lead");
    // === BEGIN FOR 1 ===
                    for (var l = 0; l < leads.length; l++) {
    // === BEGIN FOR 2 ===
                        for (var a = 0; a < leads[l].childNodes.length; a++) {
                            var lead = leads[l];
                            var t = 0;
                            var tarr = new Array();
    // FOUND THE PROBLEM!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                            for (s = 0; s < lead.childnodes.length; s++) {
    // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                            var prop = props[s];
                            if (prop.childNodes.length > 0) {
                                tarr[t] = prop.firstChild.data;
                                t++;
                            } else if (prop.tagName == "service") {
                                var varr = new Array();
                                for (var v = 0; v < prop.attributes.length; v++) {
                                    varr[v] = prop.attributes[v].value;
                                }
                            } else if (prop.tagName == "supplier") {
                                var parr = new Array();
                                for (var p = 0; p < prop.attributes.length; p++) {
                                    parr[p] = prop.attributes[p].value;
                                }
                            } else if (prop.tagName == "region") {
                                var garr = new Array();
                                for (var g = 0; g < prop.attributes.length; g++) {
                                    garr[g] = prop.attributes[g].value;
                                }
                            }
                            tarr[t] = varr.join(";");
                            t++;
                            tarr[t] = parr.join(";");
                            t++;
                            tarr[t] = garr.join(";");
                            t++;
                            larr[e] = tarr.join("|");
                            e++;
    // === END FOR 2 ===
                        }
    // === END FOR 1 ===
                    }
                    oarr[r] = larr.join(".");
                    r++;
                    alert(oarr);
                    //addorder("list_leads", oarr);
    // === END FOR 0 ===
                }
                if (xmldoc.getElementsByTagName("sql")[0]) if (xmldoc.getElementsByTagName("sql")[0].firstChild) var sql = xmldoc.getElementsByTagName("sql")[0].firstChild.data; if (sql != "") alert(sql);
    // === ELSE END IF 1 ===
            } else {
                alert('There was a problem with the request.');
            }
            document.getElementById("ajaxbg").style.visibility = "hidden";
    // === END IF 0 ===
        }
    }
    and here is the error which still stands :nuts:

    Error: syntax error
    Source File: http://localhost/fab/scripts/ajax_leads.js
    Line: 101, Column: 4
    Source Code:
    } else {
    this line is underlined in the code, but i don't see how that or anything near it could be the problem 0o
    Last edited by cGamez010; 01-02-2010 at 02:11 PM. 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.

  2. #2
    slacker3 is offline x10 Sophmore slacker3 is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    146

    Re: AJAX: battling to parse the xml data. Please Help!

    i don't even understand your question (), but xml parsing should be quite easy:

    http://www.w3schools.com/Xml/xml_parser.asp

  3. #3
    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

    Re: AJAX: battling to parse the xml data. Please Help!

    i've been through w3schools myself... i can't see the source of the error as it has to be one of those misplaced errors... i'll add some comments to try make it easier. the problem that i face now is different cos i have changed my approach to the parsing.
    i don't get where that error comes from? 0o
    Last edited by cGamez010; 01-02-2010 at 07:21 AM.
    Sincerely,
    Pierre "Greywacke" du Toit.
    Please excuse the extensive posts, I prefer to split the code into includes by category/page, rather than functions.

  4. #4
    slacker3 is offline x10 Sophmore slacker3 is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    146

    Re: AJAX: battling to parse the xml data. Please Help!

    it seems like you're doing an
    for () {
    ] else



    Code:
    // parse order tags
                var order = xmldoc.getElementsByTagName("order");
                for (var o = 0; o < order.length; o++) {
    
    ...
    ...
    
    // below is my current problem, i don't see where this error could be coming from 0o
            } else {
                alert('There was a problem with the request.');
            }
    btw,
    http://www.jslint.com/

  5. #5
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: AJAX: battling to parse the xml data. Please Help!

    The right curly brace on line 94 matches the left curly brace after the "for (var o = 0; o < order.length; o++)" statement (line 38, by my calculations). It looks like you're missing a right curly brace before line 94.

    Look for an auto-indenting, paren-matching editor. Emacs is one option; Eclipse will select blocks, which you can also use to check what bracket another bracket will match. It will help you with problems like this.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  6. #6
    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

    Re: AJAX: battling to parse the xml data. Please Help!

    no i am not slacker :happysad: the begin if, begin for, end for and else have been marked with comments
    will look into it mission
    Edit:
    mission: strange, i've repasted the function, marked the loops and ifs with comments now and i still don't see whats wrong. it really seems to be a misplaced error that.
    Last edited by cGamez010; 01-02-2010 at 08:04 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.

  7. #7
    slacker3 is offline x10 Sophmore slacker3 is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    146

    Re: AJAX: battling to parse the xml data. Please Help!

    kate never lies




    :P
    Last edited by slacker3; 01-02-2010 at 08:07 AM.

  8. #8
    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

    Unhappy AJAX: Issue Resolved, feel welcome to help with generating the array nonetheless :)

    that for is ended though. i don't trust any ide unfortunately... :happysad:
    try the latest code where i marked the ifs and fors with comments.
    i still get the error. :eek4:
    Edit:
    ps: i don't believe ur ide is highlighting the error, it is merely showing that section of code is contracted. dreamweaver does the same.
    Edit:
    okay yay success!!!

    i found the bugger, was a for loop that i forgot to take out, it was hidden in the hierarchiel tabbing. here is the updated code, but still got a bit of debugging to do, as it returned the following array:

    Code:
    .122..pierre du toit;pierre@greywacke.co.za..test subject..Test Attribute 1 = Test Value 1<br />Test Attribute 2 = Test Value 3..test message..1..2010-01-01 19:49:33..1;Test Service 1;20..6;Test Supplier 1;http://www.greywacke.co.za/;Pierre du Toit;Graphic Design & AJAX;pierre@greywacke.co.za;27729154799;800..8;Test Region 7.,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
    here is the updated function, for some reason i don't seem to be able to find the order tags first and only attribute?:

    Code:
    // xml parser
    function alertContents() {
    	if (http_request.readyState == 4) {
    		if (http_request.status == 200) {
    			var xmldoc = http_request.responseXML;
    			//alert(this.responseText);
    			var order = xmldoc.getElementsByTagName("order");
    			for (var o = 0; o < order.length; o++) {
    				var r = 0;
    				var oarr = new Array();
    				if (order[o].childNodes.length > 0) {
    					if (order[o].childNodes.length > 0) {
    						oarr[r] = order[o].getAttribute("name");
    						r++;
    						var leads = order[o].getElementsByTagName("lead");
    						for (var l = 0; l < leads.length; l++) {
    							var e = 0
    							var larr = new Array();
    							var props = leads[l].childNodes;
    //=== BEGIN PROBLEMATIC CODE ==========================
    							for (var a = 0; a < props.length; a++) {
    								if (props[a].childNodes.length > 0) {
    									var t = 0;
    									var tarr = new Array();
    									var prop = props[a];
    									if (prop.childNodes.length > 0) {
    										if (prop.firstChild.data) {
    											tarr[t] = prop.firstChild.data;
    											t++;
    										}
    									} else if (prop.tagName == "service") {
    										var varr = new Array();
    										for (var v = 0; v < prop.attributes.length; v++) {
    											varr[v] = prop.attributes[v].value;
    										}
    										tarr[t] = varr.join(";");
    										t++;
    									} else if (prop.tagName == "supplier") {
    										var parr = new Array();
    										for (var p = 0; p < prop.attributes.length; p++) {
    											parr[p] = prop.attributes[p].value;
    										}
    										tarr[t] = parr.join(";");
    										t++;
    									} else if (prop.tagName == "region") {
    										var garr = new Array();
    										for (var g = 0; g < prop.attributes.length; g++) {
    											garr[g] = prop.attributes[g].value;
    										}
    										tarr[t] = garr.join(";");
    										t++;
    									}
    									larr[e] = tarr.join(".");
    									e++;
    								}
    							}
    //=== END PROBLEMATIC CODE ==========================
    						}
    						oarr[r] = larr.join("|");
    						r++;
    						alert(oarr);
    						//addorder("list_leads", oarr);
    					}
    				}
    			}
    			//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";
    	}
    }
    okay the function parses all the tags correctly, except for the service, supplier and region tags which have attributes.

    this is the array returned by the above code:
    Code:
    Test Supplier 1,125|test;pierre@greywacke.co.za|test subject|Test Attribute 1 = Test Value 1<br />Test Attribute 2 = Test Value 3|test message|1|2010-01-02 21:13:26
    it is however missing the following values being appended:
    Code:
    |1;Test Service 1|20|6;Test Supplier 1;|http://www.greywacke.co.za/;Pierre du Toit;Graphic Design & AJAX;pierre@greywacke.co.za;27729154799;800|8;Test Region 7
    which are the attributes for the service, supplier and region tags. what's wrong thats causing it not to retrieve them? 0o

    i've highlighted the problematic section of code, please help before this has to be posted to a new thread!
    Last edited by cGamez010; 01-02-2010 at 02:15 PM. 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.

  9. #9
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: AJAX: battling to parse the xml data. Please Help!

    If only E4X was supported on more browsers.

    Try breaking down the one large function into smaller functions, each of which performs a single, well-defined task. You'll notice things like you're overwriting larr each time through the for (var l loop.
    Last edited by misson; 01-02-2010 at 02:35 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  10. #10
    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

    Re: AJAX: battling to parse the xml data. Please Help!

    the problematic area is in red. it gets the inner values, just not the attribute properties for some reason (tags service, supplier and region in the xml. i'd prefer to keep the xml parsing to one function.
    Last edited by cGamez010; 01-02-2010 at 02:38 PM. Reason: spelling
    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
Page 1 of 2 12 LastLast

Similar Threads

  1. ajax help
    By mindstorm8191 in forum Programming Help
    Replies: 2
    Last Post: 07-20-2009, 10:32 AM
  2. Suggestions for best way to build a MySQL table from raw data?
    By ParallelLogic in forum Programming Help
    Replies: 5
    Last Post: 07-05-2009, 02:39 AM
  3. Sort Data
    By secretply in forum Programming Help
    Replies: 2
    Last Post: 06-15-2009, 12:25 PM
  4. HTML help...
    By anuj_web in forum Programming Help
    Replies: 5
    Last Post: 05-08-2008, 11:22 AM
  5. AJAX Gaming Server?
    By Sup3rkirby in forum Programming Help
    Replies: 14
    Last Post: 12-26-2007, 04:14 PM

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