如何使字符串"XML安全"? [英] How do you make strings "XML safe"?
问题描述
我通过通过PHP回显将XML文档发送给AJAX调用来进行响应.为了形成这个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 或另一种替代方法是使用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屋!