2013-04-07 56 views
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]; 

} 

回答

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

評論:used TBX ML v1.5,xcode 4.6.1

+0

謝謝,我會嘗試,但是我只是討厭xml atm,把一切都改成JSON,但是讓我們看看如果我們能發現爲什麼這不起作用。 – Marckaraujo 2013-04-09 12:02:54

+0

好吧,可能對別人有幫助..無論如何,_carousel reloadData中有什麼? – nzs 2013-04-09 12:07:04

+0

這是一個圖像庫,像桌面視圖一樣工作。 – Marckaraujo 2013-04-09 12:46:31