如何使字符串XML“安全”? [英] How do you make strings XML"safe"?
问题描述
我回复一个AJAX调用通过PHP echos发送一个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“安全”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!