? , & problems in XML using PHP
I am creating a XML file from a database using PHP..
I am following a syntax something like this
$query = "SELECT * FROM t1 ORDER BY field1";
$resultID = mysql_query($query, $linkID) or die("Data not found.");
$row = mysql_fetch_assoc($resultID);
$xml_output .="\t\t\t\t\t\t<given_name>" . $row['fieldname'] . "</given_name>\n";
echo $xml_output
everything works well and almost a well formed XML is formed.. But there is a problem with some of the characters like &, ? etc., How can i overcome this?? 开发者_C百科I dont want to string replace these..
I want my XML to be off a special structure which needs to be validated against a specified schema..
Help appreciated..
Two solutions come up immediatly :
- Use
<[[CDATA
sections. No need in that case to escape special chars. - Escape those chars.
&
is equals to&
. htmlspecialchars is indeed a PHP function to transform your data. But I'd rather go with the first solution.
Use PHP's htmlspecialchars
function to encode your special characters.
精彩评论