2011-07-04 9 views
0

私はTBXMLパーサーを使用していますが、いくつかの問題があります。XMLを解析する際に配列が上書きされる(Objective-C)

次のコードを使用して、製品オブジェクトのデータをlaarzenというMutableArrayに追加します。

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

    // 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]; 
     Products *product = [[Products alloc] init]; 
     // if an author element was found 
     while (Category!= nil) { 

      // instantiate an author object 


      // get the name attribute from the author element 
      product.category = [TBXML valueOfAttributeNamed:@"DisplayName" forElement:Category]; 

      // search the author's child elements for a book element 
      TBXMLElement *xmlProduct = [TBXML childElementNamed:@"Product" parentElement:Category]; 
      int i; 
      i=0; 
      // if a book element was found 
      while (xmlProduct != nil) { 
    // extract the title attribute from the book element 
        product.productId = [TBXML valueOfAttributeNamed:@"Id" forElement:xmlProduct]; 

        // extract the title attribute from the book element 
        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 author's books array and release the resource 
        [laarzen addObject:product]; 

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

      // add our author object to the authors array and release the resource 


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

    } 


    // release resources 
    [tbxml release]; 
    [product release]; 
} 

何らかの理由で、xmlから解析された最後のノードが配列のすべてのエントリを上書きします。すなわち、最後の価格が39,99であれば、アレイ内の価格は39,99となる。私のviewDidLoadで

次のように私はlaarzen割り当てている:私のdeallocで

laarzen = [[NSMutableArray alloc] init]; 

私は再びそれを解放しています。

私は以下のなかったヘッダファイルで:

NSMutableArray *laarzen; 
@property (nonatomic, retain) NSMutableArray *laarzen; 

それはおそらくメモリの問題になりますが、私はちょうどそれを見ていません。

THNX!

答えて

5

新しい製品オブジェクトを毎回作成していない限り、アレイ内の同じ製品オブジェクトへの多数の参照を保存するだけで、価格はすべて同じ製品であるため、同じに見えます。

+0

うわー、私は馬鹿だと感じています...おそらく長い間コーディングしています。 Thnx男!!! – Jos

1

商品ごとに新しい商品を作成する場所がわかりません。商品インスタンスが1つしかないようで、同じものを何度も何度も何度も更新し続けることになります。

関連する問題