Link to home
Start Free TrialLog in
Avatar of armasmike
armasmike

asked on

jquery xml loop though each node

i am trying to loop though all the node one at a time. How do i count how many node are in the xml file

There are 7 <fromname>Chris Gannon </fromname>

i need it to count then display the first one and then the next time it loads display then the next one and so on.

How do i do that ?

// insert code for loaded event here
var fromName;
var outputField = $(this.lookupSelector("xmlOutput"));
var messageString;

$.ajax({	

	type: "GET",
	url: "sampleoutput.xml",
	dataType: "xml",
	success: function(xml) {

	  $(xml).find('greeting').each(function(){
             var name_text = $(this).find('fromname').text()
			 	
           
		messageString = "Hi "  +name_text + " ";
		outputField.html(messageString);

						 });	
				
	
				}
	});

Open in new window



and my xml

<?xml version="1.0" encoding="utf-8" ?>
<greeting>
  <fromname>Chris Gannon </fromname>
  <fromname>Chris Gannon </fromname>
  <fromname>Chris Gannon </fromname>
  <fromname>Chris Gannon </fromname>
  <fromname>Chris Gannon </fromname>
  <fromname>Chris Gannon </fromname>
  <fromname>Chris Gannon </fromname>
  
</greeting>

Open in new window

Avatar of Jon Norman
Jon Norman
Flag of United Kingdom of Great Britain and Northern Ireland image

$(this).find('fromname').length should give the number of elements.

$($(this).find('fromname')[2]) should be the third element (it's zero based)

you will need find someway of storing which one (index) you have displayed (maybe in a cookie) and then display the next.
Avatar of armasmike
armasmike

ASKER

I am using a program call Adobe edge it uses jquery as it's main code. Like ac3 is to flash i am trying try to recreate my flash animation that reads a xml file and display the text.

this works
$(this).find('fromname').length

but getting it to display one line at a time then do a +1 and go to the next line and so on.
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial