SimpleXML、CDATA 和 HTML 实体
我第一次尝试使用“简单”XML 真是费尽了心思。我正在为基于 Flash 的网站构建一个小型 CMS,内容保存在 XML 文件中。我的问题是许多复制字段都是 XML CDATA 字段。一方面,:
$xml = simplexml_load_file($file, 'SimpleXMLElement', LIBXML_NOCDATA);
我可以从该节点中提取数据,并且 CDATA 标签被剥离。我的问题是尝试使用以下方式保存数据:
file_put_contents($file, $xml->asXML());
问题是:
a) 标签用其 HTML 实体等效项进行解释。我不希望这种事发生。我认为这是来自 asXML 方法,因为即使我对 $_POST 数据执行 html_entity_decode,它仍然会被转换。
b) 由于上述原因,无法添加 CDATA 标记,因为它们的字符也已转换。
到目前为止,SimpleXML 对我来说一点也不简单。有人遇到过这个吗?
I'm tearing my hair out trying to work with "simple" XML for the first time. I'm building a small CMS for a Flash based site and the content is held in an XML file. My problem is that many of the copy fields are XML CDATA fields. on the one hand, with:
$xml = simplexml_load_file($file, 'SimpleXMLElement', LIBXML_NOCDATA);
I can pull the data out of that node and the CDATA tags are stripped. My issues come with trying to save the data with:
file_put_contents($file, $xml->asXML());
Problems are:
a) tags are interpreted with their HTML entity equivalents. I don't want this to happen. I gather this is coming from the asXML method because even if I do an html_entity_decode on the $_POST data it's still being converted.
b) because of the above, there's no way to add the CDATA tags because they also have their charachters converted.
SimpleXML so far has been anything but simple for me. Has anyone ever run into this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,这似乎正是我需要做的:
如何保留保存的 DOMDocument < as <
虽然翻阅 DOM api 手册给了我一些新东西,但我想学习以供将来使用。
Actually, this seemed to be exactly what I needed to do:
How to keep DOMDocument from saving < as <
Although pouring over the manual for the DOM api has given me something new I'd like to learn for future use.
我以前没有使用过 simpleXML,但如果您的问题是数据被保存为 XML 编码,那么 Flash 可以将 XMLEncoded 数据本机转换为 XML 对象。您可以加载数据并将其传递给新的 XML 对象,如下所示:
var ldr:URLLoader = new URLLoader( new URLRequest('./test.xml') );
ldr.addEventListener(Event.COMPLETE,handleLoaded);
函数handleLoaded(e:Event):void
{
var xml:XML = new XML(e.target.data);
}
并且您将拥有一个本机 Flash XML 对象。
如果您使用 AS2,那么您将需要使用 LoadVars 对象来加载数据,但您将需要使用
onData
(而不是onLoad
)事件来获取您的数据。 XML 编码的字符串并将其传递给 XML 对象。不确定这是否能解决您的问题,但可能会有所帮助。
I haven't used simpleXML before, but if your problem is that your data is being saved as XML encoded, then Flash can convert XMLEncoded data to XML objects natively. You can load in your data and just pass it to an new XML object like so:
var ldr:URLLoader = new URLLoader( new URLRequest('./test.xml') );
ldr.addEventListener( Event.COMPLETE, handleLoaded );
function handleLoaded( e:Event ) : void
{
var xml:XML = new XML(e.target.data);
}
and you will have a native Flash XML object.
If you are using AS2, then you will need to use the LoadVars object to load the data, but you will need to use the
onData
(notonLoad
) event to grab your XML encoded string and pass it to the XML object.not sure if this solves your problem, but it might be helpful.