开发者

Array gets overwritten when parsing XML (Objective-C)

开发者 https://www.devze.com 2023-03-17 10:13 出处:网络
I\'m using the TBXML parser but having some difficulties. I use the following code to add data from the product object to an MutableArray called laarzen.

I'm using the TBXML parser but having some difficulties.

I use the following code to add data from the product object to an MutableArray called laarzen.

- (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];
}

For some reason the last node parsed from the xml overwrites all the entries in the array. i.e. if the last price is 39,99 al prices in the array will be 39,99.

In my viewdidload i'm allocating laarzen as follows:

laarzen = [[NSMutableArray alloc] init];

In my dealloc i'm releasing it again.

In the header file I did the following:

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

It will probably be a memory issue but I just ain't seeing it.

THNX!


Unless you are making a new product object each time, you are just storing a large number of references to the same product object in your array - hence the price seeming to be the same, because they are all the same product.


I don't see where you create a new product for each item, it sounds like you only have one product instance, and end up just keep updating the same one over and over again.

0

精彩评论

暂无评论...
验证码 换一张
取 消