Reputation: 75
I'm trying to parse xml using php. I establish a connection to the database and try to fetch rows with php and then fed them into xml markers for a google map. I have a problem with streets that have an apostrophe in the name (e.g. Charles de Gaulle'a):
header('Content-type: application/xml; charset="utf-8"');
echo '<?xml version="1.0" encoding="utf-8"?>';
print "<markers>\n";
while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
$location = "<mar ";
$location .= ' LGT=' . "'" . (str_replace(',', '.', $row['Longtitude'])) . "'";
$location .= ' LAT=' . "'" . (str_replace(',', '.', $row['Latitude'])) . "'";
$location .= ' STREET=' . "'" . ($row['STREET']) . "'";
$location .= " />\n";
$output = $location;
print ($output);
}
print "</markers>\n";
This works fine when there are no streets with apostrophes inside, but with this street it returns an error:
XML Parsing Error: not well-formed
Location: http://mywebsite.com/parsexml.php
Line Number 5178, Column 141:
Question is how can I fix it so that no xml parsing error is returned and the markers are generated without any problems.
Thank you
Upvotes: 0
Views: 43
Reputation: 82
You need to use htmlentities() to encode your data. Changing your quotes will just cause problems when the other quote is used. You should always use htmlentities().
Upvotes: 1
Reputation: 57121
Your problem is the quotes are the same as round the attribute, you where ending up with STREET='Charles de Gaulle'a'
, so change them to " instead of '...
$location .= ' STREET="' . ($row['STREET']) . '"';
There may be instances though where other characters cause problems, but this should solve your immediate problem.
It would be better if you had this as an actual entity in itself (rather than an attribute), which would solve most issues. So...
<Street>Charles de Gaulle'a</Street>
Upvotes: 2