简体   繁体   English

解析 XML 时数组被覆盖(Objective-C)

[英]Array gets overwritten when parsing XML (Objective-C)

I'm using the TBXML parser but having some difficulties.我正在使用 TBXML 解析器,但遇到了一些困难。

I use the following code to add data from the product object to an MutableArray called laarzen.我使用以下代码将产品 object 中的数据添加到名为 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];
}

For some reason the last node parsed from the xml overwrites all the entries in the array.由于某种原因,从 xml 解析的最后一个节点会覆盖数组中的所有条目。 ie if the last price is 39,99 al prices in the array will be 39,99.即,如果最后一个价格是 39,99,则数组中的价格将为 39,99。

In my viewdidload i'm allocating laarzen as follows:在我的 viewdidload 中,我将 laarzen 分配如下:

laarzen = [[NSMutableArray alloc] init];

In my dealloc i'm releasing it again.在我的 dealloc 中,我再次发布它。

In the header file I did the following:在 header 文件中,我执行了以下操作:

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

It will probably be a memory issue but I just ain't seeing it.这可能是 memory 问题,但我没有看到。

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.除非您每次都在制造新产品 object,否则您只是在阵列中存储了大量对同一产品 object 的引用 - 因此价格似乎相同,因为它们都是相同的产品。

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.我看不到你在哪里为每个项目创建一个新产品,听起来你只有一个产品实例,最终只是一遍又一遍地更新同一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM