2009-05-29 42 views
2
<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文件。我有麻烦的每一项特别的缩略图使用XMLReader解析媒体RSS

这里面的数据是我的代码

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

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

是它最好使用DOMXpath,因为我是巨大的解析XML文件?我非常感谢你的建议。

+0

Hooorayy !!感谢编辑。 :) – 2009-05-29 10:32:43

回答

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

?> 

如果您对如何工作的任何问题,我会很乐意回答你的问题更多。

+0

感谢您的回复,我明白了您的想法。但现在的问题是,我有从80MB的XML解析数以千计的数据,任何想法如何可以插入到MySQL数据库批量?即。由100使用PHP? – text 2009-06-25 06:54:23