XML CDATA没有正确返回simplexml_load_string()或SimpleXMLElement() (P


XML CDATA not returning properly with simplexml_load_string() nor SimpleXMLElement() (PHP)

我可以拉回任何非cdata字段。但不是摘要字段。我已经用尽了我所知道的一切去尝试。下面是我所能得到的最接近的结果,摘要字段返回空白。当然,如果我对整个文件进行var_dump,它确实会返回,但这样我就不能访问特定的字段。请帮助!谢谢!

XML示例

<?xml version="1.0" encoding="utf-8"?>
<zatom:feed xmlns:zatom="http://www.w3.org/2005/Atom" xmlns:z4m="http://namespaces.zope.com/zc/syndication" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us">
<zatom:updated>2013-05-09T14:46:12Z</zatom:updated>
<zatom:title>Partner feeds</zatom:title>
<zatom:author><zatom:name>Zillow</zatom:name></zatom:author>
<zatom:id>urn:Zillow:wsls20130509:0000</zatom:id>
<zatom:entry z4m:content-type="zc.z4mcontent.story" z4m:status="published">
<zatom:updated>2013-05-09T14:46:12Z</zatom:updated>
<z4m:dateline term="Syndicated"/>
<z4m:source term="Zillow"/>
<zatom:rights>&#169; 2013</zatom:rights>
<zatom:category scheme="http://namespaces.zope.com/zc/z4m/section" term="real_estate_news" />
<zatom:title type="text">Harnessing the Power of Online Home Searches</zatom:title>
<zatom:summary type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><![CDATA[Harnessing the Power of Online Home Searches]]></div></zatom:summary></zatom:entry>
</zatom:feed>
PHP

$str_xml = file_get_contents('http://content.zillow.com/feeds/partners/wsls/');
    $obj_xml = new SimpleXMLElement($str_xml);
    foreach( $obj_xml->children('zatom', true)->entry as $entries ) {
        echo (string) 'Title: ' . $entries->title . '<br />';
            echo (string) 'Summary: ' . simplexml_load_string($entries->summary, null, LIBXML_NOCDATA) . "<br />'n";
    }

您的代码中有一个小问题,simplexml_load_string文档提到:

接受一个格式良好的XML字符串,并将其作为对象返回。

并且提供了一个SimpleXMLElement作为第一个参数。只需将代码中的这一行替换为以下一行:

echo (string) 'Summary: ' . simplexml_load_string($entries->summary->children()->asXML(), null, LIBXML_NOCDATA) . "<br />'n";