此元素我总是为空:<address>Rua Santo Ubaldo, 28 - São Paulo</address>
xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<music>
<principal>
<id>1</id>
<title>Woods Bar</title>
<text>Sofisticação, essa é a palavra que resume o Wood´s São Paulo. Um ambiente para o paulistano exigente, que busca por diversão, gente bonita e requinte. O sertanejo para o público seleto, com excelência em serviços e uma arquitetura diferente de tudo o que você já viu. Venha se surpreender!</text>
<thumb_url>http://www.semhora.com/xmlparser/principal/thumb.png</thumb_url>
<photo1>http://www.semhora.com/xmlparser/principal/foto1.jpg</photo1>
<photo2>http://www.semhora.com/xmlparser/principal/foto2.jpg</photo2>
<photo3>http://www.semhora.com/xmlparser/principal/foto3.jpg</photo3>
<photo4>http://www.semhora.com/xmlparser/principal/foto4.jpg</photo4>
<photo5>http://www.semhora.com/xmlparser/principal/foto5.jpg</photo5>
<flyer1>http://www.semhora.com/xmlparser/principal/flyer1.jpg</flyer1>
<flyer2>http://www.semhora.com/xmlparser/principal/flyer2.jpg</flyer2>
<address>Rua Santo Ubaldo, 28 - São Paulo</address>
<latitude>-23.596089,-46.682393</latitude>
</principal>
</music>
块:
- (void)loadURL:(NSString *)newURL{
// Create a success block to be called when the asyn request completes
TBXMLSuccessBlock successBlock = ^(TBXML *tbxmlDocument) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
// If TBXML found a root node, process element and iterate all children
if (tbxmlDocument.rootXMLElement)
{
// Obtain root element
TBXMLElement * root = tbxml.rootXMLElement;
if (root)
{
_storeArray = [NSMutableArray new];
[_storeArray removeAllObjects];
TBXMLElement * elem_PLANT = [TBXML childElementNamed:@"principal" parentElement:root];
while (elem_PLANT !=nil)
{
TBXMLElement * elem_title = [TBXML childElementNamed:@"title" parentElement:elem_PLANT];
NSString *titleName = [TBXML textForElement:elem_title];
// TBXMLElement * elem_artist = [TBXML childElementNamed:@"text" parentElement:elem_PLANT];
// NSString *artistName = [TBXML textForElement:elem_artist];
TBXMLElement * elem_thumb = [TBXML childElementNamed:@"thumb_url" parentElement:elem_PLANT];
NSString *thumbName = [TBXML textForElement:elem_thumb];
TBXMLElement * elem_photo1 = [TBXML childElementNamed:@"photo1" parentElement:elem_PLANT];
NSString *photo1Name = [TBXML textForElement:elem_photo1];
TBXMLElement * elem_photo2 = [TBXML childElementNamed:@"photo2" parentElement:elem_PLANT];
NSString *photo2Name = [TBXML textForElement:elem_photo2];
TBXMLElement * elem_photo3 = [TBXML childElementNamed:@"photo3" parentElement:elem_PLANT];
NSString *photo3Name = [TBXML textForElement:elem_photo3];
TBXMLElement * elem_photo4 = [TBXML childElementNamed:@"photo4" parentElement:elem_PLANT];
NSString *photo4Name = [TBXML textForElement:elem_photo4];
TBXMLElement * elem_photo5 = [TBXML childElementNamed:@"photo5" parentElement:elem_PLANT];
NSString *photo5Name = [TBXML textForElement:elem_photo5];
TBXMLElement * elem_flyer1 = [TBXML childElementNamed:@"flyer1" parentElement:elem_PLANT];
NSString *flyer1Name = [TBXML textForElement:elem_flyer1];
TBXMLElement * elem_flyer2 = [TBXML childElementNamed:@"flyer2" parentElement:elem_PLANT];
NSString *flyer2Name = [TBXML textForElement:elem_flyer2];
NSString *addressName = nil;
TBXMLElement * elem_address = [TBXML childElementNamed:@"address" parentElement:elem_PLANT];
if (elem_address) {
addressName = [TBXML textForElement:elem_address];
}
TBXMLElement * elem_latitude = [TBXML childElementNamed:@"latitude" parentElement:elem_PLANT];
NSString *latitudeName = [TBXML textForElement:elem_latitude];
NSDictionary *dictionary = [[NSDictionary alloc]initWithObjects:@[titleName, thumbName, photo1Name, photo2Name, photo3Name, photo4Name, photo5Name, flyer1Name, flyer2Name, addressName, latitudeName] forKeys:@[@"title", @"thumb_url", @"photo1", @"photo2", @"photo3", @"photo4", @"photo5", @"flyer1", @"flyer2", @"address", @"latitude"]];
elem_PLANT = [TBXML nextSiblingNamed:@"principal" searchFromElement:elem_PLANT];
[_storeArray addObject:dictionary];
[self startLoading:dictionary];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{ [_carousel reloadData]; }];
}
}
}
};
// Create a failure block that gets called if something goes wrong
TBXMLFailureBlock failureBlock = ^(TBXML *tbxmlDocument, NSError * error) {
NSLog(@"Error! %@ %@", [error localizedDescription], [error userInfo]);
};
// Initialize TBXML with the URL of an XML doc. TBXML asynchronously loads and parses the file.
tbxml = [[TBXML alloc] initWithURL:[NSURL URLWithString:newURL]
success:successBlock
failure:failureBlock];
}
答案 0 :(得分:1)
这不是你的“问题是什么”问题的真实答案,而是一个反馈 问题可能不在您的xml处理代码中。
作为一个锯,你有一个xml的异步查询,它在成功分支上有这个解析器处理程序。为简化测试,我在 AppDelegate 中使用了此代码段(test.xml是您在上面指定的内容):
TBXML *tbxml = [[TBXML alloc] initWithXMLFile:@"test.xml" error:nil];
TBXMLElement *root = tbxml.rootXMLElement;
if (root)
{
NSLog(@"root found" );
TBXMLElement * elem_PLANT = [TBXML childElementNamed:@"principal" parentElement:root];
while (elem_PLANT !=nil)
{
NSLog(@"iter");
NSString *addressName = nil;
TBXMLElement * elem_address = [TBXML childElementNamed:@"address" parentElement:elem_PLANT];
if (elem_address) {
addressName = [TBXML textForElement:elem_address];
NSLog(@"address=%@", addressName);
}
elem_PLANT = [TBXML nextSiblingNamed:@"principal" searchFromElement:elem_PLANT];
}
}
因此执行此操作会在控制台中产生以下结果:
2013-04-09 13:26:49.258 stack2[96042:c07] root found
2013-04-09 13:26:49.265 stack2[96042:c07] iter
2013-04-09 13:26:49.275 stack2[96042:c07] address=Rua Santo Ubaldo, 28 - São Paulo
要检查的区域可能是:1)测试具有更多“主要”元素的真实xml - 真实世界xml文件可能会发生奇怪的事情2)异步调用周围的事情,可能会尝试使用dispatch group < / p>
评论:使用TBXML v1.5,xcode 4.6.1