1

我正在使用 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;

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

提前谢谢!

4

1 回答 1

2

问题已经解决了:

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

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

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

于 2011-07-18T11:04:05.183 回答