Solved

XML JavaScipt Loop inside a Loop

Posted on 2012-03-15
3
196 Views
Last Modified: 2012-03-16
I have a strip of XML code that is looping inside a loop.

 <Stars> 
  <Star>
      <starNotAddressRecord>2</starNotAddressRecord>
      <starNotName>Not2</starNotName>
      <noRespondents>HF_TRUE</noRespondents>
      <fixedLengthAddresses>HF_FALSE</fixedLengthAddresses>
      <Notscanstar>
        <scanstarSelect>true</scanstarSelect>
        <scanstarIndex>2</scanstarIndex>
      </Notscanstar>
      <members>
        <member>@@@</member>
        <member>@@@</member>
        <member>@@@</member>
        <member>TED</member>
        <member>HF2</member>
        <member>HF1</member>
      </members>
    </Star>
    <Star>
      <starNotAddressRecord>3</starNotAddressRecord>
      <starNotName>Not3</starNotName>
      <noRespondents>HF_TRUE</noRespondents>
      <fixedLengthAddresses>HF_FALSE</fixedLengthAddresses>
      <Notscanstar>
        <scanstarSelect>true</scanstarSelect>
        <scanstarIndex>3</scanstarIndex>
      </Notscanstar>
      <members>
        <member>@@@</member>
        <member>@@@</member>
        <member>@@@</member>
        <member>@@@</member>
        <member>HF2</member>
        <member>HF1</member>
        <member>JANE</member>
      </members>
    </Star>
    <Star>
      <starNotAddressRecord>4</starNotAddressRecord>
      <starNotName>Not4</starNotName>
      <noRespondents>HF_TRUE</noRespondents>
      <fixedLengthAddresses>HF_FALSE</fixedLengthAddresses>
      <Notscanstar>
        <scanstarSelect>true</scanstarSelect>
        <scanstarIndex>4</scanstarIndex>
      </Notscanstar>
      <members>
        <member>@@@</member>
        <member>@@@</member>
        <member>@@@</member>
        <member>@@@</member>
        <member>HF1</member>
        <member>AMANDA</member>
        <member>HF2</member>
      </members>
    </Star>
  </Stars>

Open in new window


The code works fine up until the members loop. It halts everything...

		
  var x=xmlDoc.getElementsByTagName("Stars/Star");
	for (i=0;i<x.length;i++)
	{ 
		
		document.write("<br><b>Scan List Index:</b> ");
		try { document.write(x[i].getElementsByTagName("members/member")[0].childNodes[0].nodeValue); } catch(e){}; 
	    

		document.write("<p><hr><p>");		
	
	 }
		

Open in new window


How do I loop inside a loop?
0
Comment
Question by:jj1103
  • 2
3 Comments
 
LVL 18

Expert Comment

by:zc2
ID: 37726339
is it supposed to be executed in a browser?
I doubt all the browsers support that XPath-like tag serach:
xmlDoc.getElementsByTagName("Stars/Star");
0
 
LVL 18

Accepted Solution

by:
zc2 earned 500 total points
ID: 37726418
Please take a look on the following code. It enumerates the member elements. Works in IE and Mozilla. I'm still not sure is that you need or not, though.
  var star_elems=xmlDoc.getElementsByTagName("Star");
  for ( var i=0;i<star_elems.length;i++)
  {
        document.write("<h1>Scan List Index:</h1> ");
		try {
            var member_elems = star_elems[i].getElementsByTagName("members")[0].getElementsByTagName("member");
            for ( var j=0;j<member_elems.length;j++) {
    		    document.write( member_elems[j].childNodes[0].nodeValue);
            	document.write("<br/>");
     		}
		} catch(e){};
		document.write("<hr/>");

  }

Open in new window

0
 

Author Closing Comment

by:jj1103
ID: 37729324
This is exactly what I needed! Thanks! It worked great.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
XML SQL 8 25
Is there a way to use PhantomJS on a shared server? 4 17
TSQL query to generate xml 4 33
Javascript Form Change 5 18
Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question