2
這是一個初學者問題(對不起);我使用simplexml_load_string()PHP函數解析一個RSS feed,我的代碼看起來是這樣的:PHP解析器使用simplexml_load_string()
<?php
$url = "http://www.zdnet.com/blog/rss.xml";
$xml = file_get_contents($url);
$feed = simplexml_load_string($xml);
foreach($feed->channel as $channel) {
echo "Link : " . $channel->link . "<P>";
echo "Image URL : " . $channel->image->url . "<P>";
}
foreach($feed->channel->item as $item) {
echo "<HR><P>";
echo "Link : " . $item->link . "<P>";
echo "Title : " . $item->title . "<P>";
echo "Description : " . $item->description . "<P>";
echo "Date : " . $item->pubDate . "<P>";
}
?>
這是工作正常,但在XML文件中有一些標籤是這個樣子:
<media:credit role="author">
<![CDATA[ James Kendrick ]]>
</media:credit>
<media:text type="html">
<![CDATA[
<figure class="alignRight"><a href="/i/story/70/00/034218/kindle-iphone-2.jpg" target="_blank">
<img title="kindle-iphone-2" alt="kindle-iphone-2" src="http://cdn-static.zdnet.com/1.jpg">
height="237" width="200"></a><figcaption>(I
]]>
<![CDATA[
mage: James Kendrick/ ZDNet)</figcaption></figure> <p>Regular readers know I am a tablet guy
]]>
</media:text>
我如何解析這些標籤?
感謝
通常你不會在所有的解析CDATA節。只要處理純文本 – hek2mgl 2014-10-01 11:38:59
您可能會看看http://stackoverflow.com/questions/16547992/xml-cdata-not-returning-properly-with-simplexml-load-string-nor-simplexmleleme – hchr 2014-10-01 11:43:09