Return XML from Jquey Ajax php call?
===

I see various posts here on similar topics. Unfortunately these have not helped.

I make an ajax call to a php file which creates an XML file from which I populate appropriate form fields assuming data is found.

If I specify the return data to be XML as in the following I do not get a success and the alert "Could not retrieve XML file" is displayed.

    function check_Address(){
	
	alert("username=" + $("#username").attr("value"));
	
 	$.ajax({
 		type: "POST",
		url: "admin_Check_Address.php",
		data: "username=" + $("#username").attr("value"),
		dataType: "xml",                
		success: function(return_Data){
			set_Address(return_Data);
		},
		error: function(return_Data){
			alert("Could not retrieve XML file.");
		}
	});  // end ajax call
	}



However if I remove the datatype I get success function set_Address is actioned and the tagNames in return_Data are found and the field are populated correctly. i.e. 


    function check_Address(){
	
	alert("username=" + $("#username").attr("value"));
	
 	$.ajax({
 		type: "POST",
		url: "admin_Check_Address.php",
		data: "username=" + $("#username").attr("value"),
		success: function(return_Data){
			set_Address(return_Data);
		},
		error: function(return_Data){
			alert("Could not retrieve XML file.");
		}
	});  // end ajax call
	}


Here is the php:

<?php require_once("../../_includes/initialize.php"); ?>


    <?php 
    
    /*
     * Accept username from Ajax
     * Get appropriate key
     * Check for pre existing address(es)
     */
     
    if(isset($_POST["username"])){
    	
    	// retrieve user key
    
    	$sql  = "SELECT * FROM User ";
    	$sql .= "WHERE username = ";
    	$sql .= "'";
    	$sql .= $_POST['username'];	
    	$sql .= "'";
    	$sql .= " LIMIT 1;";
    	$user = User::find_By_Sql($sql);
    	$user_Key = $user[0]->get_User_Key();
    	
    	// check for an existing address or addresses
    	
    	$sql  = "SELECT * FROM Address ";
    	$sql .= "WHERE user_Key = ";
    	$sql .= "'";
    	$sql .= $user_Key;	
    	$sql .= "';";
    	$addresses = Address::find_By_Sql($sql);
    	
    	// how many addresses found
    	// if none nothing to do except set focus on address 1
    	// if 1 address returned check active and return xml to complete fields
    	// if >1 return xml to create list for user to select from
    	
    	echo "addr ct:" . count($addresses);
    
    	if (count($addresses) == 1){
    		construct_XML($addresses[0]);
    	}
    } else {
    	echo "error nothing passed";
    }
    
    
    function construct_XML($address){
    	
    	echo ("start xml");
    
    	$xml_String  = "<?xml vesrion=\"1.0\"?>";
    	$xml_String .= "<addresses>";
    	$xml_String .= "<address>";
    	$xml_String .= "<username>";
    	$xml_String .= $_POST['username'];
    	$xml_String .= "</username>";
    	$xml_String .= "<address1>";
    	$xml_String .= $address->address_1;
    	$xml_String .= "</address1>";
    	$xml_String .= "<address2>";
    	$xml_String .= $address->address_2;
    	$xml_String .= "</address2>";
    	$xml_String .= "<city>";
    	$xml_String .= $address->city;
    	$xml_String .= "</city>";
    	$xml_String .= "<county>";
    	$xml_String .= $address->county;
    	$xml_String .= "</county>";
    	$xml_String .= "<country>";
    	$xml_String .= $address->country;
    	$xml_String .= "</country>";
    	$xml_String .= "<post_Code>";
    	$xml_String .= $address->post_Code;
    	$xml_String .= "</post_Code>";
    	$xml_String .= "</address>";
    	$xml_String .= "</addresses>";
    	
    	echo $xml_String;
    	
    }
    	
    ?>


The set address function which reads the XML is:

    function set_Address(return_Data){
	
	$("#address_1").attr("value", $("address1", return_Data).text());
	$("#address_2").attr("value", $("address2", return_Data).text());
	$("#city").attr("value", $("city", return_Data).text());
	$("#county").attr("value", $("county", return_Data).text());
	$("#country").attr("value", $("country", return_Data).text());			
	$("#code").attr("value", $("post_Code", return_Data).text());
    }





I am thinking that I should be returning a file.xml rather than echoing the string but I am not sure how to do this. 

Any ideas where I am going wrong? I could simply omit the dataType and it all works fine but I would like to know why it won't recognise this as XML so I understand for next time.

Many thanks.

