My script has a javascript function that sends an http_request to a php file on my server, which then generates an XML file (output below). The same javascript function attempts to parse the XML and passes it to other functions for further processing.
I have been struggling to successfully parse the XML document.
XML Output
<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 Code Snippet
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();
}
This is my first time attempting to parse an XML document in any language, so I'm quite lost on what might be going wrong.
Any help would be greatly appreciated!
Edit: It seems my response is returning as responseText instead of responseXML.
Response Text
I am utilizing php to generate the XML page:
header("Content-Type: text/plain");
//Create the DOM
echo $xmlDoc->saveXML()
I'm still uncertain why it's not coming back as XML. Could it possibly be related to echo $xmlDoc->saveXML()?
Edit: After considering comments, it appears my issue may be with the header in the XML file. I added "alert(xmlhttp.responseText)" to my code which displays:
<?xml version="1.0"?>
<!--The Contents of my XML file-->
Could setting the encoding type resolve this issue? If so, how can I adjust my PHP code (above) to include that encoding?