Solved

XML JavaScipt Loop inside a Loop

Posted on 2012-03-15
3
198 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 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

762 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