View Single Post
Old 10-31-2012, 05:20 PM   PM User | #1
mvandall
New to the CF scene

 
Join Date: Oct 2012
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
mvandall is an unknown quantity at this point
Problems accessing xml on localhost and access to getbytagname

Hello,
I am new to javascript coding and and am trying to access an xml doc on the localhost and write its contents to a div. The xml file is not formated as a hierarchical data file unfortunately but rather as html, i.e., <table>, <tr>, <td>, etc. tags. The data I'm trying to get to is in the <td> tags - see code below. So, I'm trying too access the xml doc, "parse" the dom, get to the values in the <td> tags, and write the table of data (from the xml file) to the targetDiv. Right now, nothing is happening when I click the input button, i.e., no errors, etc., nothing happens. Thanks for any help!

Code:
<html> 
<head> 
<title>Ajax at work</title> 

<script language = "javascript">
 var XMLHttpRequestObject = false; 

var txt,x,xx,i;
 

if (window.XMLHttpRequest) {
 XMLHttpRequestObject = new XMLHttpRequest();
 
} else if (window.ActiveXObject) {
 
XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
 }
 

function getData(dataSource, divID) 
{ 
if(XMLHttpRequestObject) {
 var obj = document.getElementById(divID); 
XMLHttpRequestObject.open("GET", dataSource); 

XMLHttpRequestObject.onreadystatechange = function() 
{ 
if (XMLHttpRequestObject.readyState == 4 && 
XMLHttpRequestObject.status == 200) { 

txt="<table 

border='1'><tr><th>Score</th><th>Origin</th><th>Target</th><th>Target 

Location</th><th>Purpose</th></tr>";
 

//document.write("XML document loaded into an XML DOM Object.");

 x=xmlhttp.responseXML.documentElement.getElementsByTagName("<TD>");

 
for (i=0;i<x.length;i++)

 {

 
txt=txt + "<tr>";

 xx=x[i].getElementsByTagName("<TD>");

 {
 
try

 {
 
txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";

 }
 
catch (er)
 
{
 
txt=txt + "<td> </td>";
 
}
 }

 xx=x[i].getElementsByTagName("<td>");
 
{

 try
 
{

 txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
 
}
 
catch (er)
 
{

 txt=txt + "<td> </td>";
 
}

 }

 txt=txt + "</tr>";

 }

 txt=txt + "</table>";
 document.getElementById('targetDiv').innerHTML=txt;
 }
 

} 
} 

XMLHttpRequestObject.send(null); 
}
 }
 </script>
 </head> 

<body>
 

<H1>Fetching data...</H1>
 
<form>
 <input type = "button" value = "Display Message" 
onclick = "getData('http://localhost/IIQ/out_RSS_example2.xml', 'targetDiv')"> 
</form>
 
<div id="targetDiv">
 <p>The fetched data will go here.</p> 
</div> 


</body> 
</html>
mvandall is offline   Reply With Quote