2012-01-13 80 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; 
} 
相关问题