2013-05-10 75 views
0

我正在解析一個500MB的XML文件,用於ebay的全部「GetCategorySpecifics」並存儲在本地數據庫中。PHP XMLReader給出的值不正確

節點「MinValues」正在爲某些行插入不正確,但不是全部。如果這個節點不存在於結構中,那麼值應該是0.我已經在代碼中放入了檢查,以確保它的缺失是否設置爲0,但問題依然存在。

XML結構:

<Recommendations> 
    <CategoryID>80</CategoryID> 
     <NameRecommendation> 
      <Name>Size</Name> 
      <ValidationRules> 
        <MaxValues>1</MaxValues> 
        <MinValues>1</MinValues> 
        <SelectionMode>FreeText</SelectionMode> 
      </ValidationRules> 
      <ValueRecommendation> 
       <Value>Large</Value> 
      </ValueRecommendation> 
     </NameRecommendation> 
</Recommendations> 

PHP:

// Define XMLreader 
    $xml = new XMLReader; 
    $xml->open($xml_file,"UTF-8",LIBXML_ERR_ERROR); 
    $xml->read(); 

// Loop through file 
    while ($xml->read()) { 

     // check this isn't an ending node 
     if ($xml->nodeType != XMLReader::END_ELEMENT) { 

       // Its a new category - reset variables and define new ID 
       if ($xml->name == 'CategoryID') { 

        $MinValues = 0; $MaxValues = 1; $SelectionMode = ''; 
        $xml->read(); 

        // Validate new category ID - if invalid move to next 
        if ($xml->name == '#text' && $xml->hasValue) { 
         $CategoryID = trim($xml->value); 
         if (!is_numeric($CategoryID) || empty($CategoryID) || $CategoryID < 1) { 
          $xml->next('Recommendations'); 
         }    
        }   
        else { 
         $xml->next('Recommendations'); 
        } 

       } 


       // It's the Name tag - define Name variable 
       if ($xml->name == 'Name') { 

        $xml->read(); 
        if ($xml->name == '#text' && $xml->hasValue) { 
         $Name = mysql_real_escape_string($xml->value);  
        }   

       } 

       // It's the MaxValues tag - define MaxValues variable 
       if ($xml->name == 'MaxValues') { 

        $xml->read(); 
        if ($xml->name == '#text' && $xml->hasValue) { 
         $MaxValues = mysql_real_escape_string($xml->value); 
         if (!is_numeric($MaxValues) || empty($MaxnValues) || $MaxValues < 1) {  
          $MaxValues = 1; 
         } 
        }  

       } 

     // It's the MinValues tag - define MinValues variable 
     if ($xml->name == 'MinValues') { 
      $xml->read(); 
      if ($xml->name == '#text' && $xml->hasValue) { 
       $MinValues = mysql_real_escape_string($xml->value); 
       if (!is_numeric($MinValues) || empty($MinValues) || $MinValues < 1) {  
        $MinValues = 0; 
       } 
      }       
     } 


     // It's the SelectionMode tag - Insert new entry row into DB 
     if ($xml->name == 'SelectionMode') { 
      $xml->read(); 
      if (($xml->name == '#text') && $xml->hasValue) { 
       $SelectionMode = mysql_real_escape_string($xml->value); 
       mysql_query("INSERT INTO entry (entry_id,CategoryID,Name,MaxValues,MinValues,SelectionMode) VALUES ('','$CategoryID','$Name','$MaxValues','$MinValues','$SelectionMode')");     
       $entry_id = mysql_insert_id();     
      }     
     }  

     // It's the Value tag - Insert new values row into DB 
     if ($xml->name == 'Value') { 
      $xml->read(); 
      if (($xml->name == '#text') && $xml->hasValue) {   
       $Value = mysql_real_escape_string($xml->value); 
       mysql_query("INSERT INTO values (value_id,entry_id,CategoryID,Value) VALUES ('','$entry_id','$CategoryID','$Value')"); 
      } 
     } 
     } 
    } 
+0

有時使用':: next()'與':: readString()'結合使用不僅僅是':: read()'和' - > value'更容易http://eval.in/private/8337d6efc086a3 - 也有https://github.com/hakre/XMLReaderIterator – hakre 2013-05-12 22:18:03

回答

0

你喜歡

節點 「MinValues」 被錯誤地插入一些行,但不是所有的 解釋。如果這個節點不存在於結構中,那麼值 應該是0.我已經在代碼中放置了檢查以確保其丟失 然後設置爲0,但問題依然存在。

而且你還告訴你已經放了檢查它,但是什麼時候你不會從源頭收到MinValues元素?

所以我想建議您在閱讀您的XML時,當您發現MaxValues之後,您會發現下一個沒有MinValues的元素,那麼肯定會將它寫入'0'值的寫入過程中。

+0

如果你知道你的情況,當它錯誤插入查詢,然後你打印輸出的插入查詢'「INSERT INTO entry(entry_id ,CategoryID,Name,MaxValues,MinValues,SelectionMode)VALUES('','$ CategoryID','$ Name','$ MaxValues','$ MinValues','$ SelectionMode')「'?所以你一定會找到問題。 – NullPointer 2013-05-10 13:07:26

0

的answser:

原來NameRecommendation是可重複的,所以我需要在此節點上重新MinValues了。當你知道答案時,驚人的事情是多麼容易。