I have the following simple JavaScript parsing some XML for me:
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.open("GET","http://url.com/viewall.asp?prod_id=166746&form_id=254102&xsl=",false); xmlhttp.send(); xmlDoc=xmlhttp.responseXML;
document.write(""); var x=xmlDoc.getElementsByTagName("ROW"); for (i=0;i"); document.write(''+x[i].getElementsByTagName("FIELD_1")[0].childNodes[0].nodeValue+''); document.write(""); } document.write("");
I have a tag in the same xml called "FIELD_10" that contains info from a dropdown and I want to have this javascript "say": "if FIELD_10 is "Active" then run the output you see above"
Make sense? its just a way to put an if in there to only grab the ones that say active in the FIELD_10 and display those with the code above
Thanks peep!
I'm assuming the xml looks something like this ? :
.. url label Active url label notActive url label Active ..
Is including jQuery an option ?
I rather not use jquery, it's within a portal that is corporate built/gonverned so I'm calling all of this down in the body with edit source functionality in a text box and dont have source code access...it's hokey, but it works...
The xml output you put is an accurate depiction.
I essentially just want to be able to tell it to run through that list and only return the field data that are labeled in Field_10 as 'active'.
I think its using
if (condition here) { run through the 'row' and populate only where you see the condition met } else { document.write("no data to show dude"); }
I think it goes somewhere in the code I have, im just not good enough with this stuff to know what goes in the "condition here" for the if box and where this all gets placed in the code I have in this original first thread post
So you want to run through the xml 's and create a table of links for every that has a child with a textNode 'Active' ? And a 'no Data' cell for the ones that don't ?
var a = xmlDoc.getElementsByTagName("ROW"), sFragment = '', i;
for (i = 0; i < a.length; i++) { console.info(i); if (a[i].getElementsByTagName("FIELD_10")[0].childNodes[0].nodeValue === 'Active'){ var sUrl = a[i].getElementsByTagName("VIEW_URL")[0].childNodes[0].nodeValue, sLab = a[i].getElementsByTagName("FIELD_1")[0].childNodes[0].nodeValue; sFragment += ' ' + sLab + ''; } else { sFragment += 'no data to show dude'; } }
document.write(sFragment + '');
For every ROW containing a child FIELD_10 with 'Active', create a tr with various other child values, otherwise create a tr with some text. Haven't used xpath in a while so I'm not 100% about the syntax.
/edit on second thought the selectNodes stuff probably needs additional code for firefox etc. reverted to your queries. Ugh, jQuery would sure be nice 
thanks my man!
i actually gave a test to see if I could pull in the jquery.min.js file and it worked! so if you have a better jquery way to do this, im all ears (well, eyes cause its on the introwebs)!
I think with jQuery it can be something like this :
function onSucces (xmlDoc) { var sFragment = ''; $(xmlDoc).find('ROW').each(function (){ if ($('FIELD_10', this).text() === 'Active') { var sUrl = $('VIEW_URL', this).text(), sLbl = $('FIELD_1', this).text(); sFragment += '' + sLbl + ''; } else { sFragment += 'no data to show dude'; } }); $(sFragment+'').appendTo('body').find('tr') .mouseover(function () { $(this).css('background-color','#e1eafe'); }) .mouseout(function () { $(this).css('background-color','transparent'); }); }
$.ajax({
url: 'http://url.com/viewall.asp?prod_id=166746&form_id=254102&xsl=',
success: onSucces,
dataType: 'xml'
});
note to self: you can use jquery to walk through XML content too.
Nice, I haven't had to do it yet, but I don't think that would have occurred to me, at least, not very quickly.
Yeah I used it quite a lot to manipulate xml.
Another interesting, possibly obvious thing I learned today that I never used :
You can pass a live NodeList to jQuery, which may be useful if you have to do a lot of actions on for instance a variating amount of 's on a page.
var aSelects = document.getElementsByTagName('select'); // holds a live NodeList
// update all selects regardless of amount ..
$(aSelects).each(..);
// ..vs having to query each time to account for new tags $('select', 'body').each(..);
alright, the jquery above ended up not working, but it did lead to what appears to be a MUCH simpler path. Here's where I'm at: $(document).ready(function(){ $.ajax({ type: "GET", url: "http://url.com/viewall.asp?prod_id=166746&form_id=254102&xsl=", dataType: "xml", success: function(xml) { $(xml).find('ROW').each(function(){ var title = $(this).find('FIELD_1').text(); var url = $(this).find('URL').text(); $('').html(''+title+'').appendTo('#page-wrap');
});
}
});
});
so...I just need some help around it only showing things that have "Active" selected in FIELD_10 (that field is currently not used above). Assuming theres an "if" statement that I can place above this "var title ="
Thanks again for all the help, this seems like much better way to parse xml!
Inserting this line will do what you ask (above the 'var title=....')
if($(this).find('FIELD_10').text().toUpperCase()!='ACTIVE') { return true; }
Checks for the value of FIELD_10, ensures that if it is not equal to 'ACTIVE' (in upper case), the loop will continue to next XML node.