开发者

? , & 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 :

  1. Use <[[CDATA sections. No need in that case to escape special chars.
  2. Escape those chars. & is equals to &amp;. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜