2011-07-14 2 views
1

私はTBXMLパーサーを使用して大きなxmlファイルを解析しています。 XMLファイルは、次のように構成されている:TBXMLパーサーはEXC_BAD_ACCESSを返します

<Products> 
<Category> 
<product></product> 
</Category> 
</Products> 

そして、これはXMLを解析するために使用してi'amコードである:合計で

- (void)loadURL { 
    // Load and parse an xml string 
    Products *product = [[Products alloc] init]; 
    tbxml = [[TBXML alloc] initWithURL:[NSURL URLWithString:@"SOMELINK"]]; 

    // If TBXML found a root node, process element and iterate all children 

    // Obtain root element 
    TBXMLElement * root = tbxml.rootXMLElement; 

    // if root element is valid 
    if (root) { 
     // search for the first category element within the root element's children 

     TBXMLElement *Category = [TBXML childElementNamed:@"Category" parentElement:root]; 

     product.category = [TBXML valueOfAttributeNamed:@"DisplayName" forElement:Category]; 
     NSLog(@"%@", product.category); 
     // if an author element was found 

     while ([[TBXML valueOfAttributeNamed:@"DisplayName" forElement:Category] isEqualToString:@"Dames"]) { 





      product.category = [TBXML valueOfAttributeNamed:@"DisplayName" forElement:Category]; 

      TBXMLElement *xmlProduct = [TBXML childElementNamed:@"Product" parentElement:Category]; 


      while (xmlProduct != nil) { 
       Products *product = [[Products alloc] init]; 

       // extract the title attribute from the book element 
       product.productId = [TBXML valueOfAttributeNamed:@"Id" forElement:xmlProduct]; 

       product.material = [TBXML valueOfAttributeNamed:@"Material" forElement:xmlProduct]; 

       product.color = [TBXML valueOfAttributeNamed:@"Color" forElement:xmlProduct]; 

       product.heel = [TBXML valueOfAttributeNamed:@"Heel" forElement:xmlProduct]; 

       product.lining = [TBXML valueOfAttributeNamed:@"Lining" forElement:xmlProduct]; 

       product.subcategory = [TBXML valueOfAttributeNamed:@"DisplayName" forElement:xmlProduct]; 


       NSString * price = [TBXML valueOfAttributeNamed:@"Price" forElement:xmlProduct]; 



       // if we found a price 
       if (price != nil) { 
        // obtain the price from the book element 
        product.price = [NSNumber numberWithFloat:[price floatValue]]; 
       } 



       // add the book object to the dames array and release the resource 
       [dames addObject:product]; 
       [product release]; 


       // find the next sibling element named "xmlProduct" 
       xmlProduct = [TBXML nextSiblingNamed:@"Product" searchFromElement:xmlProduct]; 
      } 




      // find the next sibling element named "Category" 
      Category = [TBXML nextSiblingNamed:@"Category" searchFromElement:Category]; 
     } 



     while ([[TBXML valueOfAttributeNamed:@"DisplayName" forElement:Category] isEqualToString:@"Accessoires"]) { 





      product.category = [TBXML valueOfAttributeNamed:@"DisplayName" forElement:Category]; 

      TBXMLElement *xmlProduct = [TBXML childElementNamed:@"Product" parentElement:Category]; 


      while (xmlProduct != nil) { 
       Products *product = [[Products alloc] init]; 


       product.productId = [TBXML valueOfAttributeNamed:@"Id" forElement:xmlProduct]; 

       product.material = [TBXML valueOfAttributeNamed:@"Material" forElement:xmlProduct]; 

       product.color = [TBXML valueOfAttributeNamed:@"Color" forElement:xmlProduct]; 

       product.subcategory = [TBXML valueOfAttributeNamed:@"DisplayName" forElement:xmlProduct]; 

       product.heel = [TBXML valueOfAttributeNamed:@"Heel" forElement:xmlProduct]; 

       product.lining = [TBXML valueOfAttributeNamed:@"Lining" forElement:xmlProduct]; 


       NSString * price = [TBXML valueOfAttributeNamed:@"Price" forElement:xmlProduct]; 



       // if we found a price 
       if (price != nil) { 
        // obtain the price from the book element 
        product.price = [NSNumber numberWithFloat:[price floatValue]]; 
       } 




       [tassen addObject:product]; 
       [product release]; 



       xmlProduct = [TBXML nextSiblingNamed:@"Product" searchFromElement:xmlProduct]; 
      } 



      // find the next sibling element named "Category" 
      Category = [TBXML nextSiblingNamed:@"Category" searchFromElement:Category]; 

     } 


     } 
    } 

I(4つのカテゴリを有するの中央2を切り取り上記のコードでは無関係なため)。私が最初の3つのカテゴリを解析すると、すべて正常に動作しますが、すぐに私は最後に何か変わったことが起こっています。

Category = [TBXML nextSiblingNamed:@"Category" searchFromElement:Category];行は新しいカテゴリを検索しますが、1つもないのでnullを返します。終了した場合、私はパーサを言うだろうが、現時点ではパーサがクラッシュし、次のコードに(tbxml APIから)TBXML.Mファイル内EXC_BAD_ACCESSを与える:

TBXMLAttribute * attribute = aXMLElement->firstAttribute; 

私はなぜか見当もつかないが、おそらく誰かがすぐにそれを見ています...どんな助けもありがとう!

Thnxです。

+0

を働きました左手側?このステートメントで - > Category = [TBXML nextSiblingNamed:@ "Category" searchFromElement:Category]; – Raxit

+0

TBXMLElement..TBXMLElement *カテゴリ= [TBXML childElementNamed:@ "カテゴリ" parentElement:ルート]; – Jos

+0

同じ種類の問題には過去の他の人も直面しています。あなたはデバッグする必要があります。このリンクを参照してくださいhttp://stackoverflow.com/questions/5532091/iphone-sdk-help-me-about-xml-parsing-with-tbxml – Raxit

答えて

2

問題は解決されています

は、このコードが悪いのアクセスを与えた:

while ([[TBXML valueOfAttributeNamed:@"DisplayName" forElement:Category] isEqualToString:@"Accessoires"]) 

は(!= nilのカテゴリ)しばらくして、それを解決し、それが上のカテゴリのデータ型は何

関連する問題