Jump to content

Problem Parsing All The Xml Sub Elements


Greywacke
 Share

Recommended Posts

hi there,i'm sitting with a problem parsing all the xml subelements of the prospectmessages tags. the xml that needs to parse is being retrieved, but only the last element.here is the xml:

// get prospect messagesvar pctmsgs = xmldoc.getElementsByTagName("prospectmessages");			// load possible prospect messages for selected servicefor (var m = 0; m < pctmsgs.length; m++) {					// begin for possible prospect messages	var pctmsg = pctmsgs[m].getElementsByTagName("prospectmsg");		// load possible prospect message array	for (var c = 0; c < pctmsg.length; c++) {				// begin for possible prospect message		var p = 0;		var parr = new Array();		if (pctmsg[c].attributes) {					// begin if prospect message			parr[p] = pctmsg[c].getAttribute("number") +		// add prospect message number				"&&" + pctmsg[c].getAttribute("text");		// && prospect message text			p++;		}								// end if prospect message	}									// end for possible prospect message}										// end for possible prospect messagesif (parr) addprospectmessages(parr);						// populate prospect messages list

here is the addprospectmessages function:

function addprospectmessages(arr) {	alert(arr.join("\n"));	var sel = document.getElementById("list_msg");	sel.options.length = 0;	for (var i = 0; i < arr.length; i++) {		var oarr = arr[i].split("&&");					// split array element (represents an option)		var opt = document.createElement("option");			// create an option and set text to number and message		opt.text = oarr[0] + "). " + oarr[1];		opt.value = oarr[0] + "&&" + oarr[1];				// set value to number and full message		try {			sel.add(opt, null);					// standards compliant; doesn't work in IE		}		catch (ex) {			sel.add(opt);						// IE only		}	}}

when alert is enabled, to display each array item on a different line, it only displays the last item. how is it possible the first two items are not alerted or displayed in the list box?i am completely stumped. please help! i get absolutely no browser errors.

Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

You're defining p within the prospectmsg loop. Therefore, upon every loop, you have a new array that always gets just one element, in the end leaving you with just the last element. Move the array and p declarations outside of the prostepctmsg loop, like so:

	var p = 0;	var parr = new Array();	var pctmsg = pctmsgs[m].getElementsByTagName("prospectmsg");		// load possible prospect message array	for (var c = 0; c < pctmsg.length; c++) {				// begin for possible prospect message		if (pctmsg[c].attributes) {					// begin if prospect message			parr[p++] = pctmsg[c].getAttribute("number") +		// add prospect message number				"&&" + pctmsg[c].getAttribute("text");		// && prospect message text		}

Link to comment
Share on other sites

of course... thanks a mill, boen_robot! :)perhaps i need a decent night's rest...

Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

alertContents function? Could you repost your full code please (with the modifications I previously suggested being made)?[edit]Oh... so it worked I see...[/edit]

Link to comment
Share on other sites

thanks to boen_robot, this issue has been resolved :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...