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!