使用带有特殊字符的 SimpleXML 在 PHP 中创建 XML [英] Creating XML in PHP using SimpleXML with special characters

查看:37
本文介绍了使用带有特殊字符的 SimpleXML 在 PHP 中创建 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 flex 3 中处理项目,其中用户输入一些文本,这些文本将存储在 mysql 数据库中并稍后显示.我使用 XML 将数据从 flex 传输到 php,反之亦然.php 中的 SimpleXMLElement 用于创建 XML.问题是 XML 在特殊字符上的中断.

I am working on project in flex 3, where user enters some text which is to be stored in a mysql database and shown later. I am using XML for data transfer from flex to php and vice-versa. SimpleXMLElement in php is being used to create the XML. The problem is the XML breaks on special characters.

这是示例代码:

弹性:

var someXML:XML = <root><data>somedata</data><data>...</data> ... </root>
var data:Object = {};
data.xmlData = someXML;

saveDataService.send(data);

这里somedata是用户输入的数据.在php 中,我使用SimpleXMLElement 提取someXML 并将其存储到mysql.到这里没有问题.即使 somedata 有特殊字符,它也能正常工作并且数据存储正确.

Here somedata is the data entered by the user. In php, I extract the someXML using SimpleXMLElement and store it to mysql. There is no problem till here. Even if somedata has special characters its working fine and data is stored properly.

现在,在另一种情况下,从数据库中获取数据并在 flex 中显示,来自 php 的响应因错误而中断:

Now, in the other case, getting data from database and showing in flex, the response from the php breaks with an error:

SimpleXMLElement::addChild() [simplexmlelement.addchild]:未终止的实体引用

我尝试在php中使用urlencode,在flex中使用decodeURI函数,但解码后的字符串不一样,少数特殊字符仍然是%xy 解码后.

I tried using urlencode in php, and decodeURI function in flex, but the decoded string is not the same, few special characters are still %xy after decoding.

我该如何解决这个问题?

How do I solve this problem?

推荐答案

首先,如果您的 xml 中有一个悬空的 & 字符,那么您就有了 xml 语法错误.它与 SimpleXML 或任何其他解析器抱怨无关.我建议你甚至在数据进入数据库之前解决这个问题,更好的是,在它被发送到 php 服务之前.

First off, if you have a dangling & character in your xml, then you have an xml syntax error. Its nothing to do with SimpleXML or any other parser complaining. I'd suggest you fix this even before the data gets into the db, better yet, before it is sent to the php service.

Gordon 的建议有效,至少对于 &.您可能会遇到其他字符的问题,例如 <" 等,稍后您将面临几乎相同的问题.

Gordon's suggestion works, for & at least. You'll likely face the problem with other characters like <, " etc., later on and you'll be facing pretty much the same thing.

我建议您对 xml 中的 text 数据执行 htmlentities 之类的操作, 包括 xml 标签本身.以下代码取自 http://thingsthatwork.net/index.php/2008/06/26/html-entities-and-actionscript/

I'd suggest you do a htmlentities like operation on the text data in side the xml, not including the xml tags themselves. Following code taken from http://thingsthatwork.net/index.php/2008/06/26/html-entities-and-actionscript/

XML(new XMLNode(XMLNodeType.TEXT_NODE, "content with special chars like & and others")).toXMLString();

尚未测试上述内容,但它应该会返回您可以放入 xml 的 清理 内容.它应该有效地将 & 字符转换为 &amp;,所有这些字符.即,这也应该将 &gt; 的出现变成 &amp;gt;,所以要小心.

Haven't tested the above, but it should return you the sanitized content that you can put into xml. It should effectively turn & chars into &amp;, all of them. i.e., This should also turn occurences of &gt; to &amp;gt;, so be wary of that.

如果您需要其他任何东西,请告诉我.另外,你好吗,伙计!?;)

Let me know if you need anything else. Also, how ya doin' man!? ;)

编辑:哦,请记住,我使用 php 已经很长时间了,所以上面的一些信息可能有点不对.请纠正我的家伙.谢谢.

Edit: Oh, keep in mind, it has been long time since I used php, so some of the info above might be a bit off. Please correct me guys. Thanks.

这篇关于使用带有特殊字符的 SimpleXML 在 PHP 中创建 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆