使用XMLReader解析媒体RSS

tex*_*ext 2 php xml

<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>
Run Code Online (Sandbox Code Playgroud)

我决定使用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++;
        }       
    }
}
////////////////
Run Code Online (Sandbox Code Playgroud)

因为我正在解析庞大的XML文件,所以建议使用DOMXpath吗?我非常感谢你的建议.

Jor*_*nes 5

xtian,

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

我重新编写了您最初发布的内容,并捕获了元素中包含的以下元素<item>:

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

你要记住的事情是,XMLReader::localName将返回元素名减去任何xmlns声明(例如media:thumbnaillocalNamethumbnail).您将需要小心这一点,因为media:title值可能会覆盖该title值.

这是我重写的内容:

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

?>
Run Code Online (Sandbox Code Playgroud)

如果您对此有何疑问,我将非常乐意为您解答.