Jump to content

selecting xml nodes using javascript?


buddhadog
 Share

Recommended Posts

Hello everyone,I am trying to create a page which uses javascript to select xml entries. The xml file would look like this:

<?xml version="1.0"?><entries><entry><date day="10" month="10" year="2006">log info</date></entry><entry><date day="15" month="10" year="2006">log info</date></entry><entry><date day="20" month="10" year="2006">log info</date></entry></entries>

I might put more nodes into each entry, but my sticking point has been selecting the entries using javascript. I am hoping to stick to just javascript for this selection process, because it's really the only thing I know. The javascript to select nodes currently looks like the following, but I'm missing a key part of course :)

		var curdate = new Date();	var yy = curdate.getYear()+1900;	var mm = curdate.getMonth()+1;	var dd = curdate.getDate();	var x = xmlDoc.getElementsByTagName('entry');	for (j=0;j<x.length;j++) {	if (x[j].childNodes[1].[b]??[/b]>=yy) {			 display the data code }		}

I've tried getAttribute without success, but I might have done it wrong or I might be missing something else. I have javascript opening the xml file in a seperate part of my .js file, and this function would also be in there, running after the xml data is loaded. I've tested it by pulling all the entries out and displaying them, and that's working fine, it's just pulling a particular portion.My goal is to look at the current date, and then pull any entries occurring within 14 days. I have a feeling due to roll over to next months there's likely some gotchas in the javascript for that, but my focus right now is figuring out how to get those attributes correctly coded into the if statement. If anyone could slap the back of my head with the obvious I would greatly appreciate it, as I've read through many tutorials which show examples that just don't work when I try to use them.

Link to comment
Share on other sites

I've changed the date attribute to a single value, using 20061011 for October 11 2006 for example. I've used some script to create such a number for the current date, as well as a future date. I want to compare each entry's date attribute to these values, and include entries that fall between them. Here's the condition:if ((x[j].childNodes[1].getAttribute('date')>=thisday) && (x[j].childNodes[1].getAttribute('date')<=tomday))This line of code prevents IE from displaying the results, while it works in FireFox. Removing this line allows IE to display the entries, so there's something in here IE chokes on. Any ideas what the chicken bone is?

Link to comment
Share on other sites

I have a couple suggestions.To help ease the amount of processing the client computer will need to do, you might consider changing your code to something like this:

var date = x[j].childNodes[1].getAttribute('date');if ((date>=thisday) && (date<=tomday))

As to why it's not working in IE, my guess is that perhaps IE treats javascript dates a little differently. You might want to temporarily add an alert in there for debugging purposes to see if this is the case. Something like:

alert("attribute: " + date + "\nthisday: " + thisday + "\ntomday: " + tomday);

Link to comment
Share on other sites

Thanks for the headsup on IE's date! Firefox requires adding 1900 to make the date into a 2006 format, while IE does not. So I added a browser detection and changed the variable for year for IE, and now both report via alerts the correct year.Unfortunately IE still doesn't display the data, although your code change is a good one and appreciated. According to all the tutorials I've seen IE should be ok using getAttribute, but I think that's the issue.Thanks again for the date info and simpler code, now if i can just figure out this syntax issue in IE I'm set!And for reference, I figured out that FireFox and IE count childNodes differently. childNodes[1] in FF is childNodes[0] in IE. Adding another browser detection and variable reset now has both browsers working!Thanks again for your help!

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