2009-05-29 102 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