Reputation: 85
I have a javascript function that makes an http_request to a php file on my server that generates an XML file (see output below). When the XML file is returned the same javascript function parses the XML (This is where my problem lies) and passes it to other functions; which do the bulk of the processing.
So far I've been unable to parse my XML document and I can't quite figure out why.
XML
<Results><!--Root-->
<Result_Set>
<State>State</State>
<Cities>
<City>City 1</City>
<City selected="true">City 2</City>
...ETC...
</Cities>
<Zipcodes>
<Zipcode selected="true">Zipcode 1</Zipcode
<Zipcode>Zipcode 2</Zipcode>
...ETC...
</Zipcodes>
</Result_Set>
</Results>
Javascript
function GetZipInfo(zipcode){
var xmlhttp;
var x,resultSet,state,cities,zipcodes
if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
resultSet=xmlhttp.responseXML.documentElement.getElementsByTagName("Result_Set") //Function Crashes Here
for(x=0;x<resultSet.length;x++){
state=resultSet[x].getElementsByTagName("State")[0].nodeValue;
cities=resultSet[x].getElementsByTagName("Cities");
zipcodes=resultSet[x].getElementsByTagName("Zipcodes");
selectState(state)
xmlDropdown(cities, "City", "Cities")
xmlDropdown(zipcodes, "Zipcode", "Zipcodes")
}
}
}
xmlhttp.open("GET","GetZipInfo.php?Zipcode="+zipcode,true);
xmlhttp.send();
}
I have never parsed an XML document in ANY language before, so I think it's safe to say that I'm completely lost as to what's wrong.
Thank you in advance!
EDIT: It turns out that my response is coming back as responseText instead of responseXML
responseText
I am using php to create the XML page:
header("Content-Type: text/plain");
//Create the DOM
echo $xmlDoc->saveXML()
Still unsure why it's not returning as XML. Could it have something to do with echo $xmlDoc->saveXML()?
Edit: I agree with several comments that my problem is with my header in the XML file. I added the line "alert(xmlhttp.responseText)" to my code. Which displays:
<?xml version="1.0"?>
<!--The Contents of my XML file-->
Does the encoding type need to be set for this to work correctly. And if so, how can I modify my PHP code (see above) to insert that encoding?
Upvotes: 0
Views: 1848
Reputation: 665546
The MIME type of your response should be text/xml
, or something ending with +xml
(RFC 3023).
Also, you should add a XML declaration before the first line.
And last, but not least, although getElementsByTagName
is available on all elements in HTML documents, you should use document.getElementsByTagName
in XML documents:
var resultSet = xmlhttp.responseXML.getElementsByTagName("Result_Set");
Upvotes: 1
Reputation: 252
At the top of your code your missing :
<?xml version="1.0" encoding="UTF-8" ?>
Also missing ';' after these lines :
selectState(state)
xmlDropdown(cities, "City", "Cities")
xmlDropdown(zipcodes, "Zipcode", "Zipcodes")
In XML also missing '>' after :
</Zipcode
Upvotes: 0