PHP:如何处理<![CDATA [和SimpleXMLElement? [英] PHP: How to handle <![CDATA[ with SimpleXMLElement?

查看:89
本文介绍了PHP:如何处理<![CDATA [和SimpleXMLElement?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,在包含那些CDATA标记的文档上使用SimpleXMLElement时,内容始终为NULL.我该如何解决?

I noticed that when using SimpleXMLElement on a document that contains those CDATA tags, the content is always NULL. How do I fix this?

此外,对于在此处发送有关XML的垃圾内容表示抱歉.我一直在尝试使基于XML的脚本现在可以工作几个小时...

Also, sorry for spamming about XML here. I have been trying to get an XML based script to work for several hours now...

<content><![CDATA[Hello, world!]]></content>

如果您搜索"SimpleXMLElement cdata",我会尝试在Google上进行首次匹配,但这没用.

I tried the first hit on Google if you search for "SimpleXMLElement cdata", but that didn't work.

推荐答案

您可能无法正确访问它.您可以直接输出它或将其转换为字符串. (在此示例中,投射是多余的,因为无论如何回声都会自动进行投射)

You're probably not accessing it correctly. You can output it directly or cast it as a string. (in this example, the casting is superfluous, as echo automatically does it anyway)

$content = simplexml_load_string(
    '<content><![CDATA[Hello, world!]]></content>'
);
echo (string) $content;

// or with parent element:

$foo = simplexml_load_string(
    '<foo><content><![CDATA[Hello, world!]]></content></foo>'
);
echo (string) $foo->content;


使用LIBXML_NOCDATA可能会带来更好的运气:


You might have better luck with LIBXML_NOCDATA:

$content = simplexml_load_string(
    '<content><![CDATA[Hello, world!]]></content>'
    , null
    , LIBXML_NOCDATA
);

这篇关于PHP:如何处理&lt;![CDATA [和SimpleXMLElement?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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