0

我正在为 ebay 的全系列“GetCategorySpecifics”解析一个 500MB 的 XML 文件,并将其存储在本地数据库中。

节点“ 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')");
            }
        }   
        }
    }
4

2 回答 2

0

你这样解释

节点“MinValues”为某些行插入不正确,但不是全部。如果结构中不存在此节点,则该值应为 0。我已在代码中进行检查以确保其是否缺失然后设置为 0,但问题仍然存在。

而且您还告诉您已经对其进行了检查,但是当您不会MinValues从源代码中收到元素时怎么办?

所以我想建议,当你阅读你的 XML 时,当你MaxValues找到下一个元素时,你找到下一个元素,MinValues然后肯定把它放在你的写作过程中,值为“0”。

于 2013-05-10T12:16:55.163 回答
0

答案:

事实证明 NameRecommendation 是可重复的,所以我也需要在这个节点上重置 MinValues。当你知道答案时,事情是多么容易。

于 2013-06-24T13:19:00.987 回答