...

View Full Version : httpRequest with XML - Mozilla not seeing data



A1ien51
03-25-2005, 09:44 PM
I have an asp page that generates phonebook entries as an xml document based on the input of the user. The server side code is not the problem so I cut out all of the code and left the xml.


<% Response.ContentType = "text/xml" %>
<?xml version="1.0" ?>
<phonebook>
<entry>
<name>Tom Thumb</name>
<number>(123) 456-7890</number>
</entry>
<entry>
<name>Tom Stubs</name>
<number>(987) 654-3210</number>
</entry>
</phonebook>


And I have the JavaScript code that is grabbing the xml and formatting the data into the table.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Test</title>
<style type="text/css">
table{border: 1px solid black; border-collapse: collapse;}
th, td{border: 1px solid black; padding: 3px;}
</style>
<script type="text/javascript">

var reqXML;
function GrabNumber(){
url = 'phone.asp?name=' + document.Form1.user.value;
LoadXMLDoc(url);
}

function LoadXMLDoc(url){
if(window.XMLHttpRequest){
reqXML = new XMLHttpRequest();
reqXML.onreadystatechange = BuildXMLResults;
reqXML.open("GET", url, true);
reqXML.send(null);
}
else if(window.ActiveXObject){
reqXML = new ActiveXObject("Microsoft.XMLHTTP");
if (reqXML) {
reqXML.onreadystatechange = BuildXMLResults;
reqXML.open("GET", url, true);
reqXML.send();
}
}
}

function BuildXMLResults(){
if(reqXML.readyState == 4){
if(reqXML.status == 200){

var xmlDoc = reqXML.responseXML.documentElement;
var xRows = xmlDoc.getElementsByTagName('entry');

var newTable = document.createElement('TABLE');
var newBody = document.createElement('TBODY');

newTable.appendChild(newBody);

var newRow = document.createElement('TR');

for(j=0;j<xRows[0].childNodes.length;j++){
if (xRows[0].childNodes[j].nodeType != 1) continue;
var container = document.createElement('TH');
var theData = document.createTextNode(xRows[0].childNodes[j].nodeName);
container.appendChild(theData);
newRow.appendChild(container);
}

newBody.appendChild(newRow);

for(i=0;i<xRows.length;i++){
var newRow = document.createElement('TR');
for(j=0;j<xRows[i].childNodes.length;j++){
if(xRows[i].childNodes[j].nodeType != 1) continue;
var container = document.createElement('TD');
var theData = document.createTextNode(xRows[i].childNodes[j].firstChild.nodeValue);
container.appendChild(theData);
newRow.appendChild(container);
}
newBody.appendChild(newRow);
}

document.getElementById('results').innerHTML = "";
document.getElementById('results').appendChild(newTable);

}
else{
alert("There was a problem retrieving the XML data:\n" + reqXML.statusText);
}
}
}

</script>
</head>
<body>
<form name="Form1">
<input name="user" type="text" onblur="GrabNumber()" />
<div id="results"></div>
</form>
</body>
</html>


The main problem is Mozilla is not building the table since it is returning zero rows with the following lines.


var xmlDoc = reqXML.responseXML.documentElement;
var xRows = xmlDoc.getElementsByTagName('entry');


Not sure if I am attacking this part correctly here.

Eric

brothercake
03-25-2005, 10:36 PM
Check that the XML doc is being served as "text/xml" - if not, you may not get a DOM

A1ien51
03-26-2005, 05:12 PM
I tested it with a plain old xml file and it worked.....ARGH...lol

A1ien51
03-26-2005, 05:42 PM
I can get it to work with a vb.net page but not a classic ASP page. Strange.

Eric



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum