Solved

XML JavaScipt Loop inside a Loop

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
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…
Suggested Courses

617 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