tags:

views:

89

answers:

4

Hello all. I'm returning some XML from PHP to Javascript via ajax and getting some 'invalid xml' errors. The xml I'm returning looks like this:

<response>
<song>tdb2009-01-29s2s06</song>
<song>tdb2009-01-29s1s02</song>
</response>

And my javascript to parse it looks like:

 function u_handleServerResponse(){  
   //pull xml from xml response  
   var xmlResponse = xmlHttp.responseXML;

   //check to see if xml was pulled
   if(!xmlResponse || !xmlResponse.documentElement){
     throw("Invalid XML Structure:\n" + xmlHttp.responseText);
   }

   //this is for catching errors with firefox
   var rootNodeName = xmlResponse.documentElement.nodeName;

   //check for errors
   if(rootNodeName == "parsererror"){
     throw("Invalid XML Strucutre");
   }

   //get the root
   xmlRoot = xmlResponse.documentElement;

   var songArray = xmlRoot.getElementsByTagName("song");

  for(var i = 0; i < songArray.length; i++){   
   etc., etc...

And I'm getting a

Error reading the response: Invalid XML Strucutre

error. Does all this look right to you? Is the xml wrong or is it being loaded wrong? All help is greatly appreciated. Thanks in advance...

A: 

You may need to include an xml header...

<?xml version="1.0" ?>
rikh
Do you know how I could do that in PHP (where I'm building the xml)?
danwoods
A: 

In addition to the XML header pointed out by rikh, you may need to declare the Content-Type header as text/xml for the responseXML to be correctly populated.

Jani Hartikainen
I do that @ the begining of the php file which creates and returns the xml. Do I need to do it elsewhere?<?php//turn on error reportingini_set('display_errors', 'On');error_reporting(E_ALL | E_STRICT);header('Content-Type: text/xml');etc...
danwoods
A: 

Here's my php:

<?php

//turn on error reporting
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
header('Content-Type: text/xml');

//pull variables
//need to do some error checking here
$username = ($_GET['username']);

//connect with database
$con = mysql_connect("localhost","***","");
if(!$con){
  die('Could not connect: ' . mysql_error());
}

mysql_select_db("musicneverstopped", $con);
//end connecting to database

//////////////////////////////////////////

//begin processing results
$result = mysql_query("SELECT * FROM user_fav_songs WHERE username = '$username'");

//error check
if(!$result){
  die(mysql_error());
}

//check if any results were returned
if(mysql_num_rows($result) > 0){

  //if so set up the xml
  $dom = new DOMDocument();
  $response = $dom->createElement('response');
  $dom->appendChild($response);

  while($row = mysql_fetch_array($result)){

    //create song node    
    $song = $dom->createElement('song');
    $idText = $dom->createTextNode($row['unique_song_id']);
    $song->appendChild($idText);
    $response->appendChild($song);
  }//end while

  //save and echo xml
  $xmlString = $dom->saveXML(); 
  echo $xmlString;
}//end if

//////////////////////////////////////////

//close database connection
mysql_close($con);//close mysql connection


?>

Does that look right?

danwoods
+1  A: 

If you still have problems take a look at this article:
http://articles.techrepublic.com.com/5100-10878_11-6141415.html

Regards,
Bruno

Bruno Simões