2011-07-14 45 views
1

我使用TBXML解析器解析一些大的XML文件。的XML文件的结构是这样的:TBXML解析器给出了一个EXC_BAD_ACCESS

<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提前!

+0

是什么类别的数据类型上(类别=零!)左手边?在此声明 - >类别= [TBXML nextSiblingNamed:@ “类别” searchFromElement:类别]; – Raxit

+0

这是一个TBXMLElement..TBXMLElement *类别= [TBXML childElementNamed:@ “类别” parentElement:根]; – Jos

+0

过去也有一些其他人面临同样类型的问题。你需要调试。请参阅此链接http://stackoverflow.com/questions/5532091/iphone-sdk-help-me-about-xml-parsing-with-tbxml它可能对你有帮助 – Raxit

回答

2

问题解决了:

此代码给了不好的访问:

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

与同时解决它和它的工作