使用simplexml\u load\u string()的PHP解析器
这是一个初学者的问题(很抱歉);我正在使用simplexml_load_string()php函数解析RSS提要,我的代码如下所示:使用simplexml\u load\u string()的PHP解析器,php,xml,parsing,simplexml,Php,Xml,Parsing,Simplexml,这是一个初学者的问题(很抱歉);我正在使用simplexml_load_string()php函数解析RSS提要,我的代码如下所示: <?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 "
<?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>";
}
?>
<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>
频道作为$channel){
回声“链接:”$channel->Link.”;
回显“图像URL:”.$channel->Image->URL。“
”;
}
foreach($feed->channel->item as$item){
回声“
”;
回显“链接:”$item->Link.
”;
回显“标题:”.$item->Title.
”;
echo“Description:”.$item->Description.
”;
回显“日期:”.$item->pubDate.
”;
}
?>
这很好,但XML文件中有一些标记,如下所示:
<?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>";
}
?>
<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>
(一)
]]>
普通读者都知道我是个平板电脑迷
]]>
如何解析这些标记
感谢访问具有名称空间的节点,在这种情况下,您可以使用
->children()
:
$url = "http://www.zdnet.com/blog/rss.xml";
$feed = simplexml_load_file($url, null, LIBXML_NOCDATA);
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>";
$media = $item->children('media', 'http://search.yahoo.com/mrss/'); // medias
// then just loop the children
// foreach($media as $m) {}
$s = $item->children('s', 'http://www.zdnet.com/search'); // ss
}
$url=”http://www.zdnet.com/blog/rss.xml";
$feed=simplexml\u load\u文件($url,null,LIBXML\u NOCDATA);
foreach($feed->channel->item as$item){
回声“
”;
回显“链接:”$item->Link.
”;
回显“标题:”.$item->Title.
”;
echo“Description:”.$item->Description.
”;
回显“日期:”.$item->pubDate.
”;
$media=$item->children('media','http://search.yahoo.com/mrss/“);//媒体
//然后把孩子们圈起来
//foreach(媒体为百万美元){}
$s=$item->children('s','http://www.zdnet.com/search);//ss
}
通常您根本不会解析CDATA区域。只需将这些文本处理为您可能会看到的普通文本即可