2012-04-28 36 views
0

嗨我在過去兩天到處尋找&找不到任何適合我的東西。我設法從網址中提取數據,並在日誌中顯示每一個元素和值。不知道如何將TBXML網址數據添加到我的表格視圖

我無法弄清楚的一步是如何將它添加到我的「events」數組中,而不必手動搜索每個字符串,因爲它自動爲我執行(如我的日誌中所見)。

這裏是我使用loadURL方法:

- (void)loadURL { 

// Create a success block to be called when the asyn request completes 
TBXMLSuccessBlock successBlock = ^(TBXML *tbxmlDocument) { 
    NSLog(@"PROCESSING ASYNC CALLBACK"); 

    // If TBXML found a root node, process element and iterate all children 
    if (tbxmlDocument.rootXMLElement) 
     [self traverseElement:tbxmlDocument.rootXMLElement]; 

}; 

// 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:@"http://somexml.com/xml"] 
          success:successBlock 
          failure:failureBlock]; 

    events = [NSMutableArray array]; } 

這裏是我的traverseElement方法:

- (void) traverseElement:(TBXMLElement *)element { 

do { 
    // Display the name of the element 
    NSLog(@"%@",[TBXML elementName:element]); 


    // iterate all child elements of tbxml.rootXMLElement that are named "author" 
    [TBXML iterateAttributesOfElement:element withBlock:^(TBXMLAttribute *attribute, NSString *name, NSString *value) { 

     // Display name and value of attribute to the log window 
     NSLog(@"%@->%@ = %@",[TBXML elementName:element], name, value); 

    }]; 

    // if the element has child elements, process them 
    if (element->firstChild) [self traverseElement:element->firstChild]; 

    // Obtain next sibling element 
} while ((element = element->nextSibling)); 


} 

這裏是我拉的XML:

<resultsPage totalEntries="6" perPage="50" page="1" status="ok"> 
<results> 
<event uri="http://somexml.com/xml" popularity="0.863682" displayName="Radio 1's Hackney 
Weekend 2012" id="9234656" type="Festival" status="ok"> 
<location city="London, UK" lng="-0.128" lat="51.5078"/> 
<series displayName="Radio 1's Hackney Weekend"/> 
<end time="" date="2012-06-24" datetime=""/> 
<start time="" date="2012-06-23" datetime=""/> 
<performance displayName="Lucy Labeaux" billingIndex="5" id="23336188" billing="headline"> 
<artist uri="http://www.somexml.com/artistxml" displayName="Lucy Labeaux" id="1168415"> 
<identifier href="http://somexml.com.xml" mbid="4593d49a-7f67-46ba-9ec0-126bd676286f"/> 
</artist> 
</performance> 

我的日誌看起來像這樣:

PROCESSING ASYNC CALLBACK 
resultsPage 
resultsPage->totalEntries = 6 
resultsPage->perPage = 50 
resultsPage->page = 1 
resultsPage->status = ok 
results 
event 
event->uri = http://example.com 
event->popularity = 0.863682 
event->displayName = Cooled Up Week 2012 
event->id = 9234656 
event->type = Festival 
event->status = ok 
location 
location->city = London, UK 
location->lng = -0.128 
location->lat = 51.5078 
series 
series->displayName = Cooled Up Week 2012 
end 
end->time = 
end->date = 2012-06-24 
end->datetime = 
start 
start->time = 
start->date = 2012-06-23 
start->datetime = 
performance 
performance->displayName = Lucy Labeaux 
performance->billingIndex = 1 

所以,現在我已經經歷了所有元素,屬性等,我如何將它鏈接到我的表視圖和行數?我想放棄,請幫助!我非常感謝你的時間,非常感謝你!

+0

你試圖把什麼特定的數據從你的表格視圖放入? – 2012-04-28 14:23:08

+0

從事件中,我試圖拉出uri和displayName。從城市的位置,lng,&lat。從開始,時間和日期。我也試圖將我的行數分配給totalEntries的數量,但似乎無法弄清楚這一點。 – Year3000 2012-04-28 19:47:06

+0

我在想這些問題,但我不確定:'TBXMLElement * event = [TBXML childElementNamed:@「event」parentElement:element]; NSString * uri = [TBXML valueOfAttributeNamed:@「uri」forElement:event]; NSString * venue = [TBXML valueOfAttributeNamed:@「displayName」forElement:event]; [events addObject:[NSArray arrayWithObjects: [TBXML textForElement:event], uri,venue,nil]]; – Year3000 2012-04-28 20:15:57

回答

0

須藤室射頻有最好的響應

那麼你爲什麼不試試呢?自從我使用TBXML以來已經有一段時間了。從那以後,我開始直接使用libxml2(通過Cocoa包裝器),這樣我就可以使用X-path直接選擇我想要的內容而無需循環。作爲替代,你應該嘗試這個解析庫;我聽說過它簡單的優點。 github.com/ZaBlanc/RaptureXML對不起,我現在沒有時間寫這個測試用例。

相關問題