使用 XMLReader 解析媒体 RSS

2024-03-25

<rss version="2.0"
    xmlns:media="http://search.yahoo.com/mrss/">
    <channel> 
        <title>Title of RSS feed</title> 
        <link>http://www.google.com</link> 
        <description>Details about the feed</description> 
        <pubDate>Mon, 24 Nov 08 21:44:21 -0500</pubDate> 
        <language>en</language> 
        <item> 
            <title>Article 1</title> 
            <description><![CDATA[How to use StackOverflow.com]]></description>
            <link>http://youtube.com/?v=y6_-cLWwEU0</link>
            <media:player url="http://youtube.com/?v=y6_-cLWwEU0"    /> 
            <media:thumbnail url="http://img.youtube.com/vi/y6_-cLWwEU0/default.jpg"
                width="120" height="90" /> 
            <media:title>Jared on StackOverflow</media:title> 
            <media:category label="Tags">tag1,tag2</media:category> 
            <media:credit>Jared</media:credit> 
            <enclosure url="http://youtube.com/v/y6_-cLWwEU0.swf"
                length="233"
                type="application/x-shockwave-flash"/>
        </item>
    </channel>
</rss>

我决定使用 XMLReader 解析我的大型 xml 文件。我无法获取每个项目中的数据,尤其是缩略图

这是我的代码

//////////////////////////////

$itemList = array();
$i=0;
$xmlReader = new XMLReader();
$xmlReader->open('XMLFILE');
while($xmlReader->read()) {
    if($xmlReader->nodeType == XMLReader::ELEMENT) {
            if($xmlReader->localName == 'title') {
                    $xmlReader->read(); 
            $itemList[$i]['title'] = $xmlReader->value;
        }
        if($xmlReader->localName == 'description') {
            // move to its textnode / child
            $xmlReader->read(); 
            $itemList[$i]['description'] = $xmlReader->value; 

        } 
            if($xmlReader->localName == 'media:thumbnail') {
            // move to its textnode / child
            $xmlReader->read(); 
            $itemList[$i]['media:thumbnail'] = $xmlReader->value; 
                    $i++;
        }       
    }
}
////////////////

由于我正在解析巨大的 XML 文件,因此建议使用 DOMXpath 吗?我非常感谢你的建议。


xtian,

如果内存使用是您关心的问题,我建议您远离 DOM/XPath,因为它要求首先将整个文件读入内存。 XMLReader 一次只能读取一个块(可能是 8K,因为这似乎是标准的 PHP 块大小)。

我重写了您最初发布的内容,它捕获了包含在<item>元素:

  1. title
  2. description
  3. media:thumbnail
  4. media:title

你必须记住的是XMLReader::localName将返回元素名称减去任何 XMLNS 声明(例如media:thumbnail's localName is thumbnail)。您需要小心这一点,因为media:title值可能会覆盖title value.

这是我重写的内容:

<?php
define ('XMLFILE', dirname(__FILE__) . '/Rss.xml');
echo "<pre>";

$items = array ();
$i = 0;

$xmlReader = new XMLReader();
$xmlReader->open (XMLFILE, null, LIBXML_NOBLANKS);

$isParserActive = false;
$simpleNodeTypes = array ("title", "description", "media:title");

while ($xmlReader->read ())
{
    $nodeType = $xmlReader->nodeType;

    // Only deal with Beginning/Ending Tags
    if ($nodeType != XMLReader::ELEMENT && $nodeType != XMLReader::END_ELEMENT)
    {
        continue;
    }
    else if ($xmlReader->name == "item")
    {
        if (($nodeType == XMLReader::END_ELEMENT) && $isParserActive)
        {
            $i++;
        }
        $isParserActive = ($nodeType != XMLReader::END_ELEMENT);
    }

    if (!$isParserActive || $nodeType == XMLReader::END_ELEMENT)
    {
        continue;
    }

    $name = $xmlReader->name;

    if (in_array ($name, $simpleNodeTypes))
    {
        // Skip to the text node
        $xmlReader->read ();
        $items[$i][$name] = $xmlReader->value;
    }
    else if ($name == "media:thumbnail")
    {
        $items[$i]['media:thumbnail'] = array (
            "url" => $xmlReader->getAttribute("url"),
            "width" => $xmlReader->getAttribute("width"),
            "height" => $xmlReader->getAttribute("height")
        );
    }
}

var_dump ($items);

echo "</pre>";

?>

如果您对其工作原理有任何疑问,我将非常乐意为您解答。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 XMLReader 解析媒体 RSS 的相关文章

随机推荐