PHP:如何处理 <![CDATA[ 与 SimpleXMLElement?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2970602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:17:31  来源:igfitidea点击:

PHP: How to handle <![CDATA[ with SimpleXMLElement?

phpxmltagscdata

提问by Angelo

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

我注意到在SimpleXMLElement包含这些 CDATA 标签的文档上使用时,内容总是NULL. 我该如何解决?

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

另外,很抱歉在这里发送有关 XML 的垃圾邮件。我一直试图让一个基于 XML 的脚本工作几个小时......

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

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

如果您搜索“SimpleXMLElement cdata”,我在 Google 上尝试了第一次点击,但没有成功。

回答by Josh Davis

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)

您可能没有正确访问它。您可以直接输出它或将其转换为字符串。(在本例中,强制转换是多余的,因为无论如何 echo 都会自动执行)

$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;


You might have better luck with LIBXML_NOCDATA:

你可能有更好的运气LIBXML_NOCDATA

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

回答by Pradip Kharbuja

The LIBXML_NOCDATAis optional third parameter of simplexml_load_file()function. This returns the XML object with all the CDATA data converted into strings.

LIBXML_NOCDATA是可选的第三个参数simplexml_load_file()的功能。这将返回所有 CDATA 数据转换为字符串的 XML 对象。

$xml = simplexml_load_file($this->filename, 'SimpleXMLElement', LIBXML_NOCDATA);
echo "<pre>";
print_r($xml);
echo "</pre>";


Fix CDATA in SimpleXML


修复 SimpleXML 中的 CDATA

回答by breez

This did the trick for me:

这对我有用:

echo trim($entry->title);

回答by vijayrana

This is working perfect for me.

这对我来说很完美。

$content = simplexml_load_string(
    $raw_xml
    , null
    , LIBXML_NOCDATA
);

回答by Gabriel Glenn

When to use LIBXML_NOCDATA?

什么时候使用LIBXML_NOCDATA

I add the issue when transforming XML to JSON.

我在将 XML 转换为 JSON 时添加了这个问题。

$xml = simplexml_load_string("<foo><content><![CDATA[Hello, world!]]></content></foo>");
echo json_encode($xml, true); 
/* prints
   {
     "content": {}
   }
 */

When accessing the SimpleXMLElement object, It gets the CDATA :

当访问 SimpleXMLElement 对象时,它获取 CDATA :

$xml = simplexml_load_string("<foo><content><![CDATA[Hello, world!]]></content></foo>");
echo $xml->content; 
/* prints
   Hello, world!
*/

I makes sense to use LIBXML_NOCDATAbecause json_encodedon't access the SimpleXMLElement to trigger the string casting feature, I'm guessing a __toString()equivalent.

我用起来很有意义,LIBXML_NOCDATA因为json_encode不访问 SimpleXMLElement 来触发字符串转换功能,我猜是__toString()等价的。

$xml = simplexml_load_string("<foo><content><![CDATA[Hello, world!]]></content></foo>", null, LIBXML_NOCDATA);
echo json_encode($xml);
/*
 {
   "content": "Hello, world!"
 }
*/