2013-05-14 68 views
0

我可以撤回任何非CDATA字段。但不是彙總字段。我用盡了所有我知道的嘗試。下面是儘可能接近,總結字段返回空白。它確實返回,如果我做了整個文件的var_dump當然,但是我不能訪問特定的字段。請幫忙!謝謝!使用simplexml_load_string()或SimpleXMLElement()(PHP)不能正確返回XML CDATA

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"; 
    } 

回答

0

你在你的代碼中的小問題,simplexml_load_string documentation是指:

Takes a well-formed XML string and returns it as an object.

和你提供一個SimpleXMLElement作爲第一論據。只是這一次替換該行從您的代碼:

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

感謝您的快速響應,但是,當它擊中那行,就出現了錯誤,並退出。打印出第一個「標題」後,控制檯日誌實際上會引發以下錯誤...「NetworkError:500 Internal Server Error - http:// xxx /」 – user2379061 2013-05-14 18:00:03

+1

Eureka!你幾乎在那裏羅蘭多...一個小調整: 回聲(字符串)'摘要:'。 simplexml_load_string($ entries-> summary-> children() - > asXML(),null,LIBXML_NOCDATA)。 「
\ n」; $入口必須是$條目...但你做的一切都是正確的......謝謝!你讓我今天一整天都感覺很好。 – user2379061 2013-05-14 18:06:16

+0

@ user2379061我的錯,當我玩弄你的代碼時,我把'foreach'上的變量名從'$ entries'改爲'$ entry'。對於原始代碼的一致性問題,我更新了答案:) – 2013-05-14 20:01:35