問題描述
我通過 PHP 回聲向 AJAX 調用發送一個 XML 文檔來響應它.為了形成這個 XML 文檔,我遍歷了一個數據庫的記錄.問題是數據庫包含具有<"的記錄其中的符號.很自然地,瀏覽器會在該特定位置拋出錯誤.如何解決這個問題?
I am responding to an AJAX call by sending it an XML document through PHP echos. In order to form this XML document, I loop through the records of a database. The problem is that the database includes records that have '<' symbols in them. So naturally, the browser throws an error at that particular spot. How can this be fixed?
推薦答案
通過使用 htmlspecialchars轉義這些字符代碼>,或者,也許更合適的是,使用庫來構建 XML 文檔,例如 DOMDocument 或XMLWriter.
另一種選擇是使用 CDATA 部分,但是您必須注意 ]]>
的出現.
Another alternative would be to use CDATA sections, but then you'd have to look out for occurrences of ]]>
.
還要考慮到您必須遵守為 XML 文檔定義的編碼(默認為 UTF-8).
Take also into consideration that that you must respect the encoding you define for the XML document (by default UTF-8).
這篇關于你如何使字符串“XML 安全"?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!