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 つを切り取ってください)。最初の 3 つのカテゴリを解析するときはすべて正常に動作しますが、最後のカテゴリに到達するとすぐに何か奇妙なことが起こります。
行は Category = [TBXML nextSiblingNamed:@"Category" searchFromElement:Category];
新しいカテゴリを検索しようとしていますが、カテゴリがないため、null が返されます。終了した場合はパーサーと言うでしょうが、この時点でパーサーがクラッシュし、次のコードで TBXML.M ファイル (tbxml API から) に EXC_BAD_ACCESS が返されます。
TBXMLAttribute * attribute = aXMLElement->firstAttribute;
理由はわかりませんが、ここにいる誰かがすぐにそれを見るかもしれません...どんな助けも大歓迎です!
事前にthnx!