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

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

问题描述

我正在使用 flex 3 中的项目,其中用户输入要存储在 mysql 数据库,稍后显示。我使用 XML 从flex到php进行数据传输,反之亦然。正在使用中的 SimpleXMLElement 来创建 XML 。问题是特殊字符< XML 中断。



以下是示例代码:

FLEX:

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

saveDataService.send(data);

这里 somedata 用户。在 php 中,我使用 SimpleXMLElement 提取 someXML 并存储它到 mysql 。这里没有问题。即使 somedata 有特殊字符,它的工作正常并且数据被正确存储。



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


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


我试过使用 urlencode php中的和flex中的 decodeURI 函数,但是解码后的字符串不一样,很少有特殊字符仍然是% xy 解码后。



如何解决这个问题?

解决方案

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



Gordon的建议适用于& 至少。您可能会遇到像< 等其他字符的问题,稍后您将面临着几乎相同的事情。



我建议你做一个 htmlentities 类似于 text 数据放在xml中, not 包括xml标签本身。下面的代码取自 http://thingsthatwork.net/index.php/2008/06/26/html-entities-and-actionscript/ XML(new XMLNode(XMLNodeType.TEXT_NODE,带有&和其他特殊字符的内容))toXMLString(); $($) b $ b

没有测试过上述内容,但是应该返回 内容你可以把它放到xml中,它应该把& chars有效地转换成& amp; 。即,这也应该将& gt; 的出现转化为 & amp; gt; ,所以要小心。



让我知道你是否需要别的东西。另外,你怎么做男人!? ;)

编辑:哦,请记住,一点点。请纠正我的人。谢谢。


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.

Here is sample code:

FLEX:

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

saveDataService.send(data);

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.

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]: unterminated entity reference

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?

解决方案

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'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.

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();

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!? ;)

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天全站免登陆