Solved

XML JavaScipt Loop inside a Loop

Posted on 2012-03-15
3
194 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
Comment Utility
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
Comment Utility
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
Comment Utility
This is exactly what I needed! Thanks! It worked great.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article discusses how to create an extensible mechanism for linked drop downs.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now