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

cGamez010

New Member
Messages
37
Reaction score
0
Points
0
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:

cGamez010

New Member
Messages
37
Reaction score
0
Points
0
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:

slacker3

New Member
Messages
146
Reaction score
6
Points
0
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/
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
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.
 

cGamez010

New Member
Messages
37
Reaction score
0
Points
0
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:

cGamez010

New Member
Messages
37
Reaction score
0
Points
0
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. :biggrin:
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;[url]http://www.greywacke.co.za/;Pierre[/url] 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 ==========================
[COLOR="Red"]							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++;
								}
							}[/COLOR]
//=== 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:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
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:

cGamez010

New Member
Messages
37
Reaction score
0
Points
0
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:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
i'd prefer to keep the xml parsing to one function.

Put it in a single object, or hide the additional functions in the scope of another function, but monolithic functions are a bad thing. As with the larr issue, they hide problems due to being hard to read. Another issue that's hard to see (and the source of the problem with the service, supplier and region tags) is the first if (props[a].childNodes.length > 0) test will skip over these nodes. If you refactor the large function into smaller ones, this would have been readily apparent.

They also reduce code duplication, as in the sequence of if (prop.tagName blocks.

Speaking of, are you expecting tarr to have multiple elements? From the sample encoded strings, I'd guess no. If not, why are you using an array? If you did, the choice of separator (".") is problematic, as some data contains that character.

Why are you creating your own encoding? Why not use (e.g.) JSON? Why do you need to pass an encoded string to addorder? Why arrays? As it stands, the code is brittle in that it depends on the order of elements and attributes. If you use objects rather than arrays, mapping the XML tag and attribute names to property names and text nodes and attribute values to property values, the result would be more robust.

Here's a first attempt at refactoring, based on your current design. Before trying it out, consider the above questions. The code is untested; your mileage may vary.
Code:
var alertContents = (function () {
        var leadPropSep = '|',
            leadSep = '#';
        
        function prop2str(prop) {
            switch (prop.tagName) {
            case "service":  // FALLTHRU
            case "region":   // FALLTHRU
            case "supplier":
                var varr = [];
                for (var i = 0; i < prop.attributes.length; ++i) {
                    varr[i] = prop.attributes[i].value;
                }
                return varr.join(";");

            default:
                if (prop.childNodes.length > 0) {
                    if (prop.firstChild.data) {
                        return prop.firstChild.data.toString();
                    }
                }
                break;
            }
            return null;
        }
    
        function lead2array(leadXML) {
            var larr = [];
            var props = leadXML.childNodes;
            var propStr;
            for (var i=0, j=0; i < props.length; ++i) {
                propStr = prop2str(props[i]);
                if (null != propStr) {
                    larr[j++] = propStr;
                }
            }
            return larr;
        }
        function lead2str(leadXML) {
            return lead2array(leadXML).join(leadPropSep);
        }
    
        function order2array(order) {
            var oarr = [];
            if (order.childNodes.length > 0) {
                var i = 1;
                oarr[i] = order.getAttribute("name");
                var leads = order.getElementsByTagName("lead");
                var larr = [];
                for (var j = 0; j < leads.length; j++) {
                    larr.push(lead2str(leads[j]));
                }
                oarr[i++] = larr.join(leadSep);
            }
            return oarr;
        }
    
        // xml parser
        return function() {
            if (http_request.readyState == 4) {
                if (http_request.status == 200) {
                    var xmldoc = http_request.responseXML;
                    //alert(this.responseText);
                    var orders = xmldoc.getElementsByTagName("order");
                    for (var i = 0; i < orders.length; ++i) {
                        var orderStr = order2array(orders[i]);
                        alert(orderStr);
                        //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";
        }
    }
})();
 
Last edited:

cGamez010

New Member
Messages
37
Reaction score
0
Points
0
ah, thanks for pointing that out misson (hehe i sometimes don't read too well :/)- and that those with attributes get skipped.

i've fixed the script with || props[a].attributes in that if statement.

however it is supposed to join the last 3 lead properties into tarr (which includes all the lead information) which are available in the xml file. It does this now.

well i still battle wrapping my head around multidimentional arrays (a major flaw in my javascripting - i know), and the reason for the multiple code blocks is that the attributes of the last 3 lead properties, differ between the 3 tags. the alertContents function merely retrieves the xml data, the dhtml functions interpret the string array.

I'll verify the delimiters once i''ve parsed the entire xml file. the first string in the array is always the name of the order sorted by, followed by leads and their properties seperated by pipe symbols then the properties that have multiple values, are seperated by semicolons. the position of the variables in the top level array split (after the first entry) will mean what type of subvalues are available. splitting is not the problem at all, and why i don't like using IDE's is cos sure its alot easier to use but when you have to debug and go into the code - that tends to be harder than i have it. i prefer knowing exactly what happens where in the code.

the reason is so the dhtml functions can easily insert and update the elements with cross-browser code on the page, that i don't use an ide. the schema is for this page only, not to be reused on other pages. if i can get it working then i know why its working so if something changes down the line it can easily be changed right through from the top level.

below is the array received now, the total test xml file received as response (including the sql queries for debugging) is in the reply to this post. i will get this issue resolved (why it's not parsing the other leads in the orders).

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|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;780|8;Test Region 7

i'm reading that how to ask questions, and i'm sticking to it about 85%. the other 15% is due to getting stuck and panicking, then the thread takes a new direction. i'd rather update a current thread than post 3 threads in succession (can be seen as spamming). i most often do not look for the solution, i'm usually asking for a nudge in the right direction, for possibilities as to why i may not be "seeing the light". unfortunately due to my disability, i tend to get this (panicking when and not seeing the obvious) more often than most. but google is the first way i go if i don't see anything wrong. if failing on google, then i try friends over irc, if they fail then i try forums. i'd have to say this forum has been alot more friendly than wrox p2p forums (alot busier) :)
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
I'm not following most of your reasoning.

well i still battle wrapping my head around multidimentional arrays,
Is this why you're joining and later splitting? Better to learn proper techniques than continue to practice poor ones. Objects and multidimensional arrays are hierarchical, like (most) modern file systems. Think of accessing elements like paths:
Code:
var arr = [
  0, // arr[0]
  1, // arr[1]
  [  // arr[2]
    'foo', // arr[2][0]
    'bar', // arr[2][1]
    [      // arr[2][2]
      'bam',      // arr[2][2][0]
      'bug-AWWK!' // arr[2][2][1]
    ], 
    'quux' // arr[2][3]
  ], 
  3 // arr[3]
];

var obj = {
  zero: 0, // obj.zero, obj['zero']
  one: 1,  // obj.one, obj['one']
  two: {   // obj.two, obj['two']
    zero: 'bar', // obj.two.zero, obj['two'].zero, ...
    one: 'bar',  // obj.two.one, obj.two['one'], ...
    two: [       // obj.two.two, obj['two'].two, ...
      'bam',      // obj.two.two[0], obj['two']['two'][0], ...
      'bug-AWWK!' // obj.two.two[1], obj.two['two'][1], ...
    ],
    three: 'quux' // obj.two.three, obj['two']['three'], ...
  },
  three: 3 // obj.three, obj['three']
};


and the reason for the multiple code blocks is that the attributes of the last 3 lead properties, differ between the 3 tags.
The nodes might have different attributes, but the code blocks are the same modulo variable names, which means the code blocks are computationally equivalent. Notice the code blocks were combined in my last post.

the alertContents function merely retrieves the xml data, the dhtml functions interpret the string arrays.
Does this mean you join in alertContents, then split in other functions on the same page, with no network calls in between? Wasteful and needlessly complex. As a bonus, if you don't bother converting the arrays (or objects) to strings, you won't have problems with delimiters.

and why i don't like using IDE's is cos sure its alot easier to use but when you have to debug and go into the code - that tends to be harder than i have it. i prefer knowing exactly what happens where in the code.
Stepwise debugging (which is what you find in IDEs) is a little harder to learn than (e.g.) simply displaying the values of variables at various points in the code--however, live debugging of JS is done in the browser, not the IDE (see e.g. Firebug). Even when you can use an IDE to debug, you don't have to; you could merely use the IDE to edit code. Your last statement in particular makes no sense, as stepwise debugging is second only to debugging backwards in time (video) in revealing exactly what happens at each point in code evaluation.

What's your definition of an IDE?

the reason is so the dhtml functions can easily insert and update the elements with cross-browser code on the page, that i don't use an ide.
This also doesn't make sense. An IDE won't prevent code from being cross-browser, nor make functions more complex, nor interfere with the operation of code.

the schema is for this page only, not to be reused on other pages. if i can get it working then i know why its working so if something changes down the line it can easily be changed right through from the top level.
The XML schema might only be used on this page (which I find hard to believe, because something else must be producing the HTML) right now, but that might change in the future. Even if it doesn't change, you could accidentally change the order of elements or attributes in one spot. Since writing code that won't break if element or attribute order changes is at most only slightly harder, why not take the more robust approach? It can also be more understandable: property names explain their values, integer indices do not.

below is the array received now, the total test xml file received as response (including the sql queries for debugging) is in the reply to this post. i will get this issue resolved (why it's not parsing the other leads in the orders).
In the code you posted, addorder was called after each lead was added to an order (another fact hid by using a monolithic function); this behavior might be involved with this problem. The behavior wasn't a problem with the example XML as there was only one lead.

Here's how I would approach things: convert the XML to a JS object and work with that. No conversion to strings.
Code:
// not every browser is up to standard
if (! Node.ELEMENT_NODE) {
    Node.ELEMENT_NODE=1;
}

var xml2obj = (function () {
        function tagFor(node) {
            return node.tagName.toLowerCase();
        }
        function addChild(parentObj, childNode) {
            if (Node.ELEMENT_NODE != childNode.nodeType) return;
            var childObj = xml2obj(childNode);
            // don't use childObj.tag, in case childNode has a "tag" attribute
            var name = tagFor(childNode);
            if (null == parentObj[name]) {
                parentObj[name] = childObj;
            } else {
                if (! parentObj[name] instanceof Array) {
                    parentObj[name] = [ parentObj[name] ];
                }
                parentObj[name].push(childObj);
            }
        }
        function xml2obj(xml) {
            var obj = {tag: tagFor(xml)};
            if (xml.attributes) {
                var attrs = xml.attributes;
                for (var i=0; i<attrs.length; ++i) {
                    obj[attrs[i].name] = attrs[i].value;
                }
            }
            var children = xml.childNodes;
            for (var i=0; i<children.length; ++i) {
                addChild(obj, children[i]);
            }
            return obj;
        }
        return xml2obj;
    })();

Call addOrder(xml2obj(xmldoc.getElementsByTagName("order"))) and you've got your orders as JS objects. If you really need objects or arrays as strings, use JSON.stringify, which is supported in FF 3.5, Safari 4, Chrome 3 and IE8. Earlier versions of Safari and Chrome might also have native support for JSON encoding, depending on when the JSON object was added to WebKit (which looks to be 7 months ago, so probably not). For other browsers, test for the existence of JSON and JSON.stringify, defining them if they don't exist using one of the many implementations (on FF 3, you can wrap around nsIJSON).
 
Last edited:

cGamez010

New Member
Messages
37
Reaction score
0
Points
0
i'll give it a try on the next project, and read your tips tomorrow when i get up. for this project i'd rather stick with what i am familiar with. however, trying to get more than 1 lead processed, there is another one of those proverbial horizon to horizon brick walls and i don't see the doors. i need sleep now, so i'll try again tomorrow after reading your post. :) this is the code i have sofar, the comments are more clear, to the right of most lines. this function is set to the onreadystatechange event of the http_request object.

just a pity the tabs use 4 spaces in dreamweaver, and more or less in this forums code blocks.

Code:
// xml parser
function alertContents() {
    if (http_request.readyState == 4) {                                                            // filter state changed
        if (http_request.status == 200) {                                                        // filter errors
            var xmldoc = http_request.responseXML;                                                // get response
            //alert(this.responseText);                                                            // alert xml data
            var order = xmldoc.getElementsByTagName("order");                                    // load possible order tags
            for (var o = 0; o < order.length; o++) {                                            // begin for possible orders
                var r = 0;
                var oarr = new Array();
                if (order[o].attributes) {                                                                    // begin if order
                    oarr[r] = order[o].getAttribute("name");                                                // add sort order order name to 1st item in Order ARRay
                    r++;
                    var leads = order[o].getElementsByTagName("lead");                                        // load possible lead tags
                    for (var l = 0; l < leads.length; l++) {                                                // begin for possible leads
                        if (leads[l].childNodes.length > 0) {                                                // begin if lead
                            var e = 0
                            var larr = new Array();
                            var props = leads[l].childNodes;                                                // load possible props
                            for (var a = 0; a < props.length; a++) {                                        // begin for possible props
                                if (props[a].childNodes > 0 || props[a].attributes) {                        // begin if prop
                                    var t = 0;
                                    var tarr = new Array();
                                    var prop = props[a];                                                    // load property
                                    if (prop.childNodes.length > 0) {                                        // if a lead property
                                        tarr[t] = prop.firstChild.data;                                        // collect 7 lead properties as first items in properTy ARRay
                                        t++;
                                    } else if (prop.tagName == "service") {                                    // otherwise if a collection of service properties
                                        var varr = new Array();
                                        for (var v = 0; v < prop.attributes.length; v++) {
                                            if (prop.attributes[v]) varr[v] = prop.attributes[v].value;        // collect 3 serVice ARRay items
                                        }
                                        tarr[t] = varr.join(";");                                            // add service properties as 8th item in properTy ARRay
                                        t++;
                                    } else if (prop.tagName == "supplier") {                                // otherwise if a collection of supplier properties
                                        var parr = new Array();
                                        for (var p = 0; p < prop.attributes.length; p++) {
                                            if (prop.attributes[p]) parr[p] = prop.attributes[p].value;    // collect 8 suPplier ARRay items
                                        }
                                        tarr[t] = parr.join(";");                                            // add supplier properties as 9th item in properTy ARRay
                                        t++;
                                    } else if (prop.tagName == "region") {                                    // otherwise if a collection of region properties
                                        var garr = new Array();
                                        for (var g = 0; g < prop.attributes.length; g++) {
                                            if (prop.attributes[g]) garr[g] = prop.attributes[g].value;        // collect 2 reGion ARRay items
                                        }
                                        tarr[t] = garr.join(";");                                            // add region properties as 10th item in properTy ARRay
                                        t++;
                                    }
                                    larr[e] = tarr.join(".");                                                // add properTies ARRay to Lead Array
                                    e++;
                                }                                                                            // end if prop
                            }                                                                                // end for possible props
                        }                                                                                    // end if 
                    }                                                                                        // end for possible leads
                    oarr[r] = larr.join("|");                                                                // add leads as 2nd item in Order ARRay
                    r++;
                    alert(oarr);                                                                            // alert Order ARRay
                    //addorder("list_leads",oarr);                                                            // add sort ORDER and associated leads
                }                                                                                            // end if order
            }                                                                                                // end for possible orders
            // alert processed mysql queries
            //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";
    }
}
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
i'll give it a try on the next project, and read your tips tomorrow when i get up. for this project i'd rather stick with what i am familiar with.
Some things, such as using JSON.stringify and the functional programming techniques I used for xml2obj (closures and first class functions), can easily wait. You should hold off on others, such as using objects or multi-dimensional arrays, only if you're under a deadline, since they will result in a simpler implementation. Still others, such as breaking down large tasks (and functions) into smaller ones, shouldn't wait for anything. Every problem you've been having wouldn't have happened if you weren't dealing with a monolithic function.

The approaches I'm recommending aren't a matter of theory or of producing elegant code for the sake of aesthetics, they're very pragmatic, designed to reduce errors during development. Bad designs take longer to develop, have more problems and mean more work for you.

Rest well.
 
Top