我正在使用TBXML分析器来分析一些大的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文件(来自tbxml api)中的以下代码中给出EXC_BAD_ACCESS:TBXMLAttribute * attribute = aXMLElement->firstAttribute;
我不知道为什么,但是也许有人在这里马上看到了。任何帮助将不胜感激!
提前thnx!
最佳答案
问题已经解决了:
此代码提供了错误的访问权限:
while ([[TBXML valueOfAttributeNamed:@"DisplayName" forElement:Category] isEqualToString:@"Accessoires"])
用while(类别!= nil)解决了它,它起作用了
关于iphone - TBXML分析器提供EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6691371/