1
我總是空該元素:<address>Rua Santo Ubaldo, 28 - São Paulo</address>
TBXML textForElement不解析一個元素
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];
}
謝謝,我會嘗試,但是我只是討厭xml atm,把一切都改成JSON,但是讓我們看看如果我們能發現爲什麼這不起作用。 – Marckaraujo 2013-04-09 12:02:54
好吧,可能對別人有幫助..無論如何,_carousel reloadData中有什麼? – nzs 2013-04-09 12:07:04
這是一個圖像庫,像桌面視圖一樣工作。 – Marckaraujo 2013-04-09 12:46:31