Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

looping javascript to pull data from XML tags 2

Status
Not open for further replies.

joshbates15

Technical User
Jun 3, 2005
8
US
I'm so close to figuring this out, but I need a little help with the looping to get my data. What I'm having problems with is putting the data from the XML tags "<saying>" into a list.

The XML file looks like this...
Code:
<?xml version="1.0" encoding="UTF-8"?>
<aaronsays>
	<date>June 15, 2005</date>
	<saying>If he was Catholic, I would be his godfather.</saying>
	<saying>Tell everyone I'm the reigning champ!</saying>
	<saying>I think my mom stole my clothes.</saying>
</aaronsays>

This is the code from my html/javascript...
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] >
<head>
    <title>Aaron Says</title>
    <style type="text/css">
    .date {display:block;}
    </style>
    <script type="text/javascript">
    var xmlDoc;
    
    function loadXMLDoc()
    {
        if (window.XMLHttpRequest)
        {
            xmlDoc = new XMLHttpRequest();
            xmlDoc.onreadystatechange = processXMLChange;
            xmlDoc.open("GET", "content.xml", true);
            xmlDoc.send(null);
        }
        else if (window.ActiveXObject)
        {
            xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
            if (xmlDoc)
            {
                xmlDoc.onreadystatechange = processXMLChange;
                xmlDoc.open("GET", "content.xml", true);
                xmlDoc.send();
            }
        }
    }
    
    function processXMLChange()
    {
        if (xmlDoc.readyState == 4)
        {
            if (xmlDoc.status == 200)
            {
                document.getElementById("content").innerHTML = "";
                var aaronSays = xmlDoc.responseXML.documentElement;
                var xmlDate = aaronSays.getElementsByTagName("date")[0].firstChild.data;
                var xmlSaying = aaronSays.getElementsByTagName("saying");
                [red]var listSaying = document.createElement("ul");[/red]
                [red]for (i=0;i<xmlSaying.length;i++)[/red]
                [red]{[/red]
                   [red] var container = document.createElement("li");[/red]
                    [red]var sayingElm = xmlSaying[i];[/red]
                    [red]var theData = document.createTextNode(sayingElm.firstChild.nodeValue);[/red]
                    [red]container.appendChild(theData);[/red]
                [red]}[/red]
                [red]listSaying.appendChild(container);[/red]
                [red]var div = document.getElementById("content").innerHTML = "<span>"+xmlDate+"</span><br />"+listSaying;[/red]
            }
            else
            {
                alert("Error reading XML Data, please be patient while we fix the problem");
                window.location="[URL unfurl="true"]http://aaronsays.com"[/URL]
            }
        }
    }
    </script>
</head>
<body onload="loadXMLDoc()">
    <div id="header">Header</div>
    
    <div id="content"></div>
    
    <div id="footer">Footer</div>
</body>
</html>

any help or suggestions would be very welcomed! Thanks!
 
[RED]
var div = document.getElementById("content").innerHTML = "<span>"+xmlDate+"</span><br />"+listSaying;[/RED]

u cannot add an element(object) to a string. ie

[qoute]
var listSaying = document.createElement("ul");
[/quote]

try....
Code:
divel = document.getElementById("content");
if (divel)
   {
   divel.innerHTML = "<span>"+xmlDate+"</span><br />";
   divel.appendChild(listSaying);   
   }
divel is a name i used for div....not a good idea to name ur variables same as html tags :)


---------------------------
WORD OR VOTE TO THE WISE IS ENUFF...;)
 
using dom methods:
Code:
    function processXMLChange()
    {
        if (xmlDoc.readyState == 4)
        {
            if (xmlDoc.status == 200)
            {
                document.getElementById("content").innerHTML = "";
                var aaronSays = xmlDoc.responseXML.documentElement;
                var xmlDate = aaronSays.getElementsByTagName("date")[0].firstChild.nodeValue;
                var xmlSaying = aaronSays.getElementsByTagName("saying");
                var listSaying = document.createElement("ul");
                for (i=0;i<xmlSaying.length;i++)
                {
                    var container = document.createElement("li");
                    var sayingElm = xmlSaying[i];
                    var theData = document.createTextNode(sayingElm.firstChild.nodeValue);
                    container.appendChild(theData);
                    listSaying.appendChild(container);
                }
                
                var contentDiv = document.getElementById("content");
                var dateContainer = document.createElement("span");
                dateContainer.appendChild( document.createTextNode(xmlDate) );
                contentDiv.appendChild(dateContainer);
                contentDiv.appendChild(listSaying);
            }
            else
            {
                alert("Error reading XML Data, please be patient while we fix the problem");
                for (p in xmlDoc) alert(p + " = " + xmlDoc[p]);
                window.location="[URL unfurl="true"]http://aaronsays.com"[/URL]
            }
        }
    }

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
oops... you can remove the line
for (p in xmlDoc) alert(p + " = " + xmlDoc[p]);

was just for debugging

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top