TBXML Parser提供EXC_BAD_ACCESS

时间:2011-07-14 09:55:09

标签: iphone objective-c ios tbxml

我正在使用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个类别)。当我解析前三个类别时,一切正常,但是一旦到达最后,就会发生一些奇怪的事情。

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

TBXMLAttribute * attribute = aXMLElement->firstAttribute;

我不知道为什么,但也许有人在这里立刻看到它......任何帮助都会受到赞赏!

Thnx提前!

1 个答案:

答案 0 :(得分:2)

问题解决了:

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

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

用while(category!= nil)解决它并且它有效