Problems parsing XML in Javascript

Foxx

New Member
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\[code\]<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>\[/code\]Javascript\[code\]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();}\[/code\]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 responseXMLresponseText I am using php to create the XML page:\[code\]header("Content-Type: text/plain");//Create the DOMecho $xmlDoc->saveXML()\[/code\]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:\[code\]<?xml version="1.0"?><!--The Contents of my XML file-->\[/code\]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?
 
Back
Top