javascript won't execute nested for loop

Posted by mcdwight6 on Stack Overflow See other posts from Stack Overflow or by mcdwight6
Published on 2012-09-17T03:34:46Z Indexed on 2012/09/17 3:37 UTC
Read the original article Hit count: 171

Filed under:
|
|
|
|

thanks in advance for all your help!

i'm fairly new to javascript, but i have a fairly strong background in java, so i thought i would try it out on this project i'm working on.

essentially, what i'm trying to do is read data from an xml file and create the html code for the page i'm making. i used the script from w3schools found here. I've altered it and gotten it to pull the data from my own xml and even to do the more basic generation of the html code i need.

Here's the html i'm using inside <script> tags:

var s = swDoc.getElementsByTagName("planet");
var plShowsArr = s[i].getElementsByTagName("show");
var plGamesArr = s[i].getElementsByTagName("videoGame");
    for (i=0;i<s.length;i++)
        {   // test section all works
            document.write("<div><table border = \"1\">");
            document.write("<tr><td>"+  s[i].getElementsByTagName("showText")[0].childNodes[0].nodeValue + "</td><td>" + s[i].getElementsByTagName("showUrl")[0].childNodes[0].nodeValue    + "</td></tr>");
            document.write("<tr><td>" + s[i].getElementsByTagName("gameText")[0].childNodes[0].nodeValue + "</td><td>" + s[i].getElementsByTagName("gameUrl")[0].childNodes[0].nodeValue + "</td></tr>");
            document.write("</tr></table></div>");
            // end test section

            document.write("<div class=\"appearances-row\"><ol class=\"shows\">shows list");
            for(j=0;j<plShows.length;j++){
                document.write("nested for");
                var showUrl = s[i].getElementsByTagName("showUrl")[j].childNodes[0].nodeValue;
                var showText = s[i].getElementByTagName("showText")[j].childNodes[0].nodeValue;
                document.write("<li><a href=\""+showUrl+"\">"+showText+"</a></li>");
            }

the code breaks at the nested for loop at the end, where it finished the document.write and prints "shows list" to the page, but then never gets to the document.write inside.

if it helps, the xml contains a list of planets from the star wars universe organized like this:

<planets>
<planet>
    <planetName>planet</planetName>
    <description>some text</description>
    <appearances>
        <show>
            <showUrl>url</showUrl>
            <showText>hyperlink text</showText>
        </show>
        <videoGame>
            <gameUrl>url</gameUrl>
            <gameText>hyperlink text</gameText>
        </videoGame>
    </appearances>
    <locationsOfInterest>
        <location>location name</location>
    </locationsOfInterest>
    <famousCharactersRelatedTo>
        <character>a character</character>
    </famousCharactersRelatedTo>
    <externalLinks>
        <link>
            <linkUrl>url</linkUrl>
            <linkText>hyperlink text</linkText>
        </link>
    </externalLinks>
</planet>

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about html