?
Solved

XML JavaScipt Loop inside a Loop

Posted on 2012-03-15
3
Medium Priority
?
201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 19

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 19

Accepted Solution

by:
zc2 earned 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…

719 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