【问题标题】:TBXML Parser gives a EXC_BAD_ACCESSTBXML Parser 给出 EXC_BAD_ACCESS
【发布时间】:2011-10-05 04:53:20
【问题描述】:

我正在使用 TBXML Parser 来解析一些大的 xml 文件。 xml文件的结构如下:

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

这是我用来解析 xml 的代码:

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

        }


        }
    }

我总共有 4 个类别(由于与上面的代码无关,删掉了中间的 2 个类别)。当我解析前 3 个类别时,一切正常,但当我到达最后一个类别时,发生了一些奇怪的事情。

Category = [TBXML nextSiblingNamed:@"Category" searchFromElement:Category]; 行将搜索一个新类别,但由于没有,它将返回 null。如果完成,我会说解析器,但此时解析器崩溃并在以下代码的 TBXML.M 文件(来自 tbxml api)中给出 EXC_BAD_ACCESS:

TBXMLAttribute * attribute = aXMLElement->firstAttribute;

我不知道为什么,但也许这里有人马上看到了...任何帮助将不胜感激!

提前谢谢!

【问题讨论】:

  • 左侧类别的数据类型是什么?在这个语句中 -> Category = [TBXML nextSiblingNamed:@"Category" searchFromElement:Category];
  • 这是一个 TBXMLElement..TBXMLElement *Category = [TBXML childElementNamed:@"Category" parentElement:root];
  • 过去其他人也遇到过相同类型的问题。你需要调试。看这个链接stackoverflow.com/questions/5532091/…可能对你有帮助
  • 我不知道我现在到底要做什么,我需要如何调试正确知道我应该在哪里寻找?与其他有问题的人不同,我知道哪里出错了,但不知道为什么会出错。谢谢

标签: iphone objective-c ios tbxml


【解决方案1】:

问题解决了:

这段代码提供了错误的访问权限:

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

用 while (category !=nil) 解决了它,它工作了

【讨论】:

    猜你喜欢
    • 2011-12-16
    • 2022-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多