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

发布于 2024-11-18 13:46:50 字数 2897 浏览 9 评论 0原文

我正在使用 TBXML 解析器,但遇到一些困难。

我使用以下代码将产品对象中的数据添加到名为 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:

laarzen = [[NSMutableArray alloc] init];

在我的 dealloc 中,我再次释放它。

在头文件中,我执行了以下操作:

NSMutableArray *laarzen;
@property (nonatomic, retain) NSMutableArray *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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雅心素梦 2024-11-25 13:46:50

除非您每次都创建一个新的产品对象,否则您只是在数组中存储对同一产品对象的大量引用 - 因此价格看起来是相同的,因为它们都是相同的产品。

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.

风流物 2024-11-25 13:46:50

我不知道你在哪里为每个项目创建一个新产品,听起来你只有一个产品实例,最终只是不断地一遍又一遍地更新同一个产品。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文