I'm getting data from a database in PHP/mysql, which has invalid characters, such as &. I'm creating the XML as follows:

```
$stmt->bind_result($foo)|| fail('An error occurred: MySQL bind_result', $db->error);

$foo=htmlspecialchars($foo, ENT_XML1, 'UTF-8');

$xmlstr = "<?xml version="1.0" encoding="UTF-8" standalone="yes">
<xml>";
while ($stmt->fetch())
{ $xmlstr.="
             <RECORD>
               <FOO>$foo</FOO>
             </RECORD>";}
  $xmlstr.="
  </xml>";

$stmt->close();

echo $xmlstr;

```

I'm trying to receive the data and put in an array with javascript - but it gives me the error Invalid XML format. I'm receiving random records and sometimes it does work. So it seems to me that the issue is in the characters coming from the database.

The error occurs here in JavaScript:

```
var foo;
var  formData = "label="+label;  //Name value Pair

 $.ajax({
            url: './php/foo.php',
            type: 'POST',
            data : formData,
            dataType: 'xml',
            success: function(returnedXMLResponse){

                $('RECORD', returnedXMLResponse).each(function(){
                     foo = $('FOO', this).text();

                })
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) { 
                    console.log("Status: " + textStatus); console.log("Error: " + errorThrown); 
}
       });

```