2012-01-13 31 views
2

我試圖使用iPhone上的Xpath從here中提取天氣信息。到目前爲止,它解析所有的數據,但我堅持如何提取內容並將其顯示在表格中。 這是我到目前爲止有:如何在iPhone上顯示Xpath

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[ @"http://aviationweather.gov/adds/metars/?station_ids=1234&std_trans=translated&chk_metars=on&hoursStr=most+recent+only&submitmet=Submit"stringByReplacingOccurrencesOfString:@"1234" withString:self.title]]]; 

TFHpple * doc  = [[TFHpple alloc] initWithHTMLData:data]; 
NSArray * elements = [doc searchWithXPathQuery:@"//table[1]//tr"]; 
NSLog(@"%@", elements); 
TFHppleElement * element = [elements objectAtIndex:0]; 
[element content];    // Tag's innerHTML 
[element tagName];    // "a" 
[element attributes];   // NSDictionary of href, class, id, etc. 
[element objectForKey:@"href"]; // Easy access to single attribute 

如果有人需要看看它的輸出到目前爲止,讓我知道。

感謝, 安德魯

+1

在Google上查看「UITableView教程」,例如, http://www.google.com/search?client=safari&rls=zh-CN&q=uitableview+tutorial&ie=UTF-8&oe=UTF-8 – 2012-01-13 21:11:50

+0

它不是顯示我不明白的表格,它組織數據以便我可以顯示它。謝謝 – Boos1993 2012-01-13 21:16:05

+0

我想我不明白。你想展示什麼數據?如果你可以把你的數據放到一個數組中,那麼你可以把它放到一個表中。看起來你已經在'elements'中有了這個數組,這只是一個循環遍歷數組來製作表格單元的問題。 – 2012-01-13 21:29:03

回答

1

我有我的一點你在,並沒有不到哪去了同樣的問題,但我最終執行這些代碼。希望它可以幫助仍然有一點需要使它正常工作,但對我開發的應用程序的性質做這是我可以給你的。它並不僅僅是你真正需要的代碼的實際實現。

#import "XPathQuery.h" 


NSMutableArray *weatherArray = [[NSMutableArray arrayWithArray:0]retain]; // Initilize the NSMutableArray can also be done with just an NSArray but you will have to change the populateArray method. 
NSString *xPathLookupQuery = @"//table[1]//tr"; // Path in xml 
nodes = PerformXMLXPathQuery(data, xPathLookupQuery); // Pass the data in that you need to search through 
[self populateArray:weatherArray fromNodes:nodes]; // To populate multiple values into array. 

session = [[self fetchContent:nodes] retain]; // To populate a single value and returns value. 

- (void)populateArray:(NSMutableArray *)array fromNodes:(NSArray *)nodes 
{ 
for (NSDictionary *node in nodes) { 
    for (id key in node) { 
     if ([key isEqualToString:@"nodeContent"]) { 
      [array addObject:[node objectForKey:key]]; 
     } 
    } 
} 
} 

您只需要上面的代碼或下面的代碼,除非您同時需要。

- (NSString *)fetchContent:(NSArray *)nodes 
{ 
NSString *result = @""; 
for (NSDictionary *node in nodes) { 
    for (id key in node) { 
     if([key isEqualToString:@"nodeContent"]) { 
      result = [node objectForKey:key]; 
     } 
    } 
} 
return result; 
}