gpt4 book ai didi

iphone - TBXML 解析器给出 EXC_BAD_ACCESS

转载 作者:行者123 更新时间:2023-11-29 11:21:48 25 4
gpt4 key购买 nike

我正在使用 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;

我不知道为什么,但也许这里有人会马上看到它...任何帮助将不胜感激!

提前致谢!

最佳答案

问题解决了:

此代码给出了错误的访问权限:

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

用 while (category !=nil) 解决了它,它起作用了

关于iphone - TBXML 解析器给出 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6691371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com