使用simplexml_load_string()和SimpleXMLElement()(PHP)无法正确返回XML CDATA

时间:2013-05-14 16:12:29

标签: php xml cdata

我可以撤回任何非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";
    }

1 个答案:

答案 0 :(得分:0)

您的代码中存在小问题,simplexml_load_string documentation指的是:

  

采用格式良好的XML字符串并将其作为对象返回。

并且您提供了一个SimpleXMLElement作为第一个参数。只需用您的代码替换代码中的那一行:

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