1

I'm using the TBXML Parser to parse some big xml file. The xml file is structured like this:

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

And this is the code which i'am using to parse the 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];

        }


        }
    }

In total I have 4 categories (Cut out the middle 2 of them because of irrelevant in the code above). All works fine when I parse the first 3 categories, but as soon I get to the last there is happening something strange.

The Category = [TBXML nextSiblingNamed:@"Category" searchFromElement:Category]; line is going to search for a new category, but since there isn't one, it will return null. I would say the parser if finished, but at this moment the parser crashes and gives an EXC_BAD_ACCESS in the TBXML.M file (from the tbxml api) on the following code:

TBXMLAttribute * attribute = aXMLElement->firstAttribute;

I have no clue why, but maybe someone here sees it right away... Any help would be appreciated!

Thnx in advance!

Jos
  • 2,421
  • 5
  • 26
  • 30
  • What is the datatype of Category On the left hand side ? In this statement -> Category = [TBXML nextSiblingNamed:@"Category" searchFromElement:Category]; – Raxit Jul 14 '11 at 10:13
  • It's an TBXMLElement..TBXMLElement *Category = [TBXML childElementNamed:@"Category" parentElement:root]; – Jos Jul 14 '11 at 10:14
  • Same type of problem is faced by some other person in past as well. You need to debug. See this link http://stackoverflow.com/questions/5532091/iphone-sdk-help-me-about-xml-parsing-with-tbxml It might be helpful to you – Raxit Jul 14 '11 at 10:41
  • I don't know what I exactly have to do right now, how do I need to debug right know and where should I look for? Unlike the other person with the problem, I know where it goes wrong, but don't know why it goes wrong. Thnx – Jos Jul 14 '11 at 11:28

1 Answers1

2

The problem is solved:

This code gave the bad access:

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

Resolved it with while (category !=nil) and it worked

Jos
  • 2,421
  • 5
  • 26
  • 30