Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用XMLReader解析媒体RSS_Php_Xml - Fatal编程技术网

Php 使用XMLReader解析媒体RSS

Php 使用XMLReader解析媒体RSS,php,xml,Php,Xml,既然我正在解析巨大的XML文件,那么使用DOMXpath是否明智?我真的很感谢你的建议。xtian 如果您担心内存使用,我建议您不要使用DOM/XPath,因为它要求首先将整个文件读入内存。XMLReader一次只读取一个块(可能是8K,因为这似乎是标准的PHP块大小) 我重新编写了您最初发布的内容,它捕获了元素中包含的以下元素: 标题 说明 媒体:缩略图 媒体:标题 您必须记住的是XMLReader::localName将返回元素名减去任何XMLNS声明(例如media:thumbnail的l

既然我正在解析巨大的XML文件,那么使用DOMXpath是否明智?我真的很感谢你的建议。

xtian

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

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

  • 标题
  • 说明
  • 媒体:缩略图
  • 媒体:标题
  • 您必须记住的是
    XMLReader::localName
    将返回元素名减去任何XMLNS声明(例如
    media:thumbnail
    localName
    thumbnail
    )。您需要注意这一点,因为
    媒体:title
    值可能会覆盖
    title

    以下是我重新撰写的内容:

    //////////////////////////////
    
    $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++;
            }       
        }
    }
    ////////////////
    
    
    

    如果您有任何关于这项工作原理的问题,我将非常乐意为您解答。

    Hooorayy!!谢谢您的编辑。:)谢谢你的回复,我知道你的想法了。但现在的问题是,我从80MB的XML中解析了数千个数据,你知道如何将这些数据批量插入mysql数据库吗?例如,通过100使用PHP?
    //////////////////////////////
    
    $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++;
            }       
        }
    }
    ////////////////
    
    <?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>";
    
    ?>