php – 如何使用simplexml解析RSS中的标记

我的RSS从http://rss.cnn.com/rss/edition.rss的结构是:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?>
<?xml-stylesheet type="text/css" media="screen" href="http://rss.cnn.com/~d/styles/itemcontent.css"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
  <channel>
    <title><![CDATA[CNN.com - RSS Channel - Intl Homepage - News]]></title>
    <description><![CDATA[CNN.com delivers up-to-the-minute news and information on the latest top stories, weather, entertainment, politics and more.]]></description>
    <link>http://www.cnn.com/intl_index.html</link>
    ...

    <item>
      <title><![CDATA[Russia responds to claims it has damaging material on Trump]]></title>
      <description><![CDATA[The Kremlin denied it has compromising information about US President-elect Donald Trump, describing the allegations as "pulp fiction".]]></description>
      <link>http://www.cnn.com/2017/01/11/politics/russia-rejects-trump-allegations/index.html</link>
      <guid isPermaLink="true">http://www.cnn.com/2017/01/11/politics/russia-rejects-trump-allegations/index.html</guid>
      <pubDate>Wed, 11 Jan 2017 14:44:49 GMT</pubDate>
      <media:group>
        <media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-super-169.jpg" height="619" width="1100" />
        <media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-large-11.jpg" height="300" width="300" />
        <media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-vertical-large-gallery.jpg" height="552" width="414" />
        <media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-video-synd-2.jpg" height="480" width="640" />
        <media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-live-video.jpg" height="324" width="576" />
        <media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-t1-main.jpg" height="250" width="250" />
        <media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-vertical-gallery.jpg" height="360" width="270" />
        <media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-story-body.jpg" height="169" width="300" />
        <media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-t1-main.jpg" height="250" width="250" />
        <media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-assign.jpg" height="186" width="248" />
        <media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-hp-video.jpg" height="144" width="256" />
      </media:group>
    </item>
    ...

  </channel>
</rss>

如果您使用simplexml解析此XML,如下所示:

  $rss = simplexml_load_file($url, null, LIBXML_NOCDATA);

  $rssjson = json_encode($rss);
  $rssarray = json_decode($rssjson, TRUE);

你会看到< media:content>在$rssarray项目中丢失了.所以我找到了一个带有“命名空间”解决方案的tutorial.但是,在示例中,作者使用:

foreach ($xml->channel->item as $item) { ... }

但我正在使用(出于某些原因不能使用foreach):

$rssjson = json_encode($rss);
$rssarray = json_decode($rssjson, TRUE);

所以我修改了我的案例的解决方案,如下所示:

  $rss = simplexml_load_file($url, null, LIBXML_NOCDATA);
  $namespaces = $rss->getNamespaces(true); // get namespaces

  $rssjson = json_encode($rss);
  $rssarray = json_decode($rssjson, TRUE);

  if (isset($rssarray['channel']['item'])) {
    foreach ($rssarray['channel']['item'] as $key => $item) {

      $media_content = $rss->channel->item[$key]->children($namespaces['media']);
      foreach($media_content as $tag) {

        $tagjson = json_encode($tag);
        $tagarray = json_decode($tagjson, TRUE);

      }

    }
  }

但它不起作用.对于我在$tagarray中获得的每个项目,结果是具有以下结构的数组:

Array(
  'content' => array(
     '0' => array(null),
     '1' => array(null),
     ...
     '11' => array(null),
   )
)

这是一个数组,其数量与< media:content>的数量一样多.标签,但每个项目都是空的.我需要获取每个项目的url属性.我做错了什么并得到一个空阵列?

解决方法:

标签实际上是空的:

<media:content ... />
                   ^^

信息包含在属性中,可以使用SimpleXMLElement::attributes()获取,例如:

$rss = simplexml_load_file($url, null, LIBXML_NOCDATA);
$namespaces = $rss->getNamespaces(true);
$media_content = $rss->channel->item[0]->children($namespaces['media']);
foreach($media_content->group->content as $i){
    var_dump((string)$i->attributes()->url);
}

我怀疑问题来自JSON技巧. SimpleXML动态生成所有类和属性(它们不是常规PHP类),这意味着您不能完全依赖标准PHP功能,如print_r()或json_encode().如果你在上面的循环中插入它,这会得到说明:

var_dump($i, json_encode($i), (string)$i->attributes()->url);
object(SimpleXMLElement)#2 (0) {
}
string(2) "{}"
string(91) "http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-super-169.jpg"
...
上一篇:如何在Windows 8应用程序中使用c#添加facebook rss feed?


下一篇:打造属于自己的 RSS 源(二)-RSS 源