www.webdeveloper.com
Results 1 to 3 of 3

Thread: Some XML shows up in wrong element, some in the right one

Hybrid View

  1. #1
    Join Date
    Mar 2006
    Posts
    8

    Some XML shows up in wrong element, some in the right one

    I am reading an XML file and looping through the contents, displaying node values in <li>. The problem I am having is the the first part of my output string is showing up in he wrong element. Subsequent text shows up in the correct place.

    Here is some sample XML:

    Code:
    <node>
            <name>Stability</name>
            <state>Low
                <state-prob>.15</state-prob>
            </state>
            <state>Moderate
                <state-prob>.25</state-prob>
            </state>
            <state>High
                <state-prob>.60</state-prob>
            </state>
        </node>
        
        <node>
            <name>Free/Fair Elections</name>
            <state>Yes
                <state-prob>.6</state-prob>
            </state>
            <state>No
                <state-prob>.4</state-prob>
            </state>
        </node>
        
        <node>
            <name>Regulatory Compliance</name>
            <state>Yes
                <state-prob>.8</state-prob>
            </state>
            <state>No
                <state-prob>.2</state-prob>
            </state>
            
        </node>

    And here is my JS:

    Code:
    <!DOCTYPE html>
    
    <html>
    <head>
        
        <style>
            #button{
                float: left;
                width:50%;
            }
            #team{
                float: right;
                width:50%;
                vertical-align: top;
            }
            
        </style>
        <title>XMLHTTPRequest</title>
        
        <script src="http://code.jquery.com/jquery-latest.min.js"></script>
            
        <script>
            $(document).ready(function(){
                xhr = new XMLHttpRequest();
                xhr.onreadystatechange = function(){
                    if (xhr.readyState == 4 && xhr.status == 200) {
                        xmlDoc = xhr.responseXML;
                        var bn = xmlDoc.getElementsByTagName("node");
                        var html = "";
                        for (i = 0; i < bn.length; i++){
                            html +=
                            "<li>" + xmlDoc.getElementsByTagName("name")[i].childNodes[0].nodeValue + " " +
                            xmlDoc.getElementsByTagName("state")[i].childNodes[0].nodeValue + " " +
                            xmlDoc.getElementsByTagName("state-prob")[i].childNodes[0].nodeValue + "</li>";
                            
                        }
                        document.getElementById("nodes").innerHTML = html
                    }
                }
                xhr.open("GET", "bn-my-test-data.xml", true);
                
            });
        </script>
    </head>
    
    <body>
        <section id="left">
            <h1>Hi Ria</h1>
            <p>How are you today?</p>
        <div id="button">
            
           <button onclick="xhr.send()">Ria Click Me!</button>
        </div>   
        </section>
        
        <section id="right">
        <div id="nodes">
        
      
            
        </div>
        </section>
    
    </body>
    </html>

    Any insight on what I am doing wrong would be greatly appreciated.

  2. #2
    Join Date
    Mar 2006
    Posts
    8
    I misspoke above it's the last "node" that is showing up in the wrong place. So Regulatory Compliance is showing up in the left section.

  3. #3
    Join Date
    Mar 2006
    Posts
    8
    I fixed it. Thanks anyway

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles