I used to have a good working XML file on my site (source for a google map). Until a few days ago got this error:
This page contains the following errors: error on line 2 at column 10896: EntityRef: expecting ';' Below is a rendering of the page up to the first error.
I have not made changes to the php file in that time period. Also did some database checks, all seems to be in order (the database connection is working fine).
I read that a common cause for this is using & instead of &. But in the parseToXML
there is a replace function for that, so that couldn't be the cause I was thinking.
Any tips?
Thank you!
Mark
require("credentials.php");
function parseToXML($htmlStr)
{
$xmlStr=str_replace('<','<',$htmlStr);
$xmlStr=str_replace('>','>',$xmlStr);
$xmlStr=str_replace('"','"',$xmlStr);
$xmlStr=str_replace("'",''',$xmlStr);
$xmlStr=str_replace("&",'&',$xmlStr);
return $xmlStr;
}
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, name, address, lat, lng, descrip FROM people";
$result = $conn->query($sql);
header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<markers>';
$ind=0;
// Iterate through the rows, printing XML nodes for each
while($row = $result->fetch_assoc()) {
// Add to XML document node
echo '<marker ';
echo 'id="' . $row['id'] . '" ';
echo 'name="' . parseToXML($row['name']) . '" ';
echo 'address="' . parseToXML($row['address']) . '" ';
echo 'lat="' . $row['lat'] . '" ';
echo 'lng="' . $row['lng'] . '" ';
echo 'descrip="' . $row['descrip'] . '" ';
echo '/>';
$ind = $ind + 1;
}
// End XML file
echo '</markers>';
Rather than manually writing out a string it is far easier and more reliable to generate your XML using the various tools designed for the job. PHP has the
DOMDocument
class which makes tasks like this very straightforward and requires fewer lines of code too ;-/ It also has the benefit of not stuttering if the text content contains chevrons or other characters